116 lines
5.0 KiB
JavaScript
116 lines
5.0 KiB
JavaScript
|
|
"use strict";
|
|||
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|||
|
|
if (k2 === undefined) k2 = k;
|
|||
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|||
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|||
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|||
|
|
}
|
|||
|
|
Object.defineProperty(o, k2, desc);
|
|||
|
|
}) : (function(o, m, k, k2) {
|
|||
|
|
if (k2 === undefined) k2 = k;
|
|||
|
|
o[k2] = m[k];
|
|||
|
|
}));
|
|||
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|||
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|||
|
|
}) : function(o, v) {
|
|||
|
|
o["default"] = v;
|
|||
|
|
});
|
|||
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|||
|
|
var ownKeys = function(o) {
|
|||
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|||
|
|
var ar = [];
|
|||
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|||
|
|
return ar;
|
|||
|
|
};
|
|||
|
|
return ownKeys(o);
|
|||
|
|
};
|
|||
|
|
return function (mod) {
|
|||
|
|
if (mod && mod.__esModule) return mod;
|
|||
|
|
var result = {};
|
|||
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|||
|
|
__setModuleDefault(result, mod);
|
|||
|
|
return result;
|
|||
|
|
};
|
|||
|
|
})();
|
|||
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|||
|
|
exports.DocumentFormattingProvider = void 0;
|
|||
|
|
const vscode = __importStar(require("vscode"));
|
|||
|
|
// 文档格式化提供者类
|
|||
|
|
class DocumentFormattingProvider {
|
|||
|
|
// 提供整个文档的代码格式化功能
|
|||
|
|
async provideDocumentFormattingEdits(document, options, token) {
|
|||
|
|
const edits = [];
|
|||
|
|
const lines = [];
|
|||
|
|
// 逐行处理文档内容
|
|||
|
|
for (let i = 0; i < document.lineCount; i++) {
|
|||
|
|
const line = document.lineAt(i);
|
|||
|
|
lines.push(line.text);
|
|||
|
|
}
|
|||
|
|
// 格式化文档
|
|||
|
|
const formattedLines = this.formatDocument(lines, options);
|
|||
|
|
// 创建编辑操作
|
|||
|
|
const fullRange = new vscode.Range(document.positionAt(0), document.positionAt(document.getText().length));
|
|||
|
|
edits.push(vscode.TextEdit.replace(fullRange, formattedLines.join('\n')));
|
|||
|
|
return edits;
|
|||
|
|
}
|
|||
|
|
// 格式化文档
|
|||
|
|
formatDocument(lines, options) {
|
|||
|
|
const formattedLines = [];
|
|||
|
|
let indentLevel = 0;
|
|||
|
|
for (let i = 0; i < lines.length; i++) {
|
|||
|
|
let line = lines[i];
|
|||
|
|
const trimmedLine = line.trim();
|
|||
|
|
if (trimmedLine.length === 0) {
|
|||
|
|
// 空行保持原样或删除
|
|||
|
|
formattedLines.push('');
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
// 检查是否需要减少缩进(遇到右大括号等)
|
|||
|
|
if (trimmedLine.startsWith('}') || trimmedLine.startsWith(')') || trimmedLine.startsWith(']')) {
|
|||
|
|
indentLevel = Math.max(0, indentLevel - 1);
|
|||
|
|
}
|
|||
|
|
// 添加适当的缩进
|
|||
|
|
const indent = this.createIndent(indentLevel, options);
|
|||
|
|
const formattedLine = indent + trimmedLine;
|
|||
|
|
formattedLines.push(formattedLine);
|
|||
|
|
// 检查是否需要增加缩进(遇到左大括号等)
|
|||
|
|
if (trimmedLine.endsWith('{') || trimmedLine.endsWith('(') || trimmedLine.endsWith('[')) {
|
|||
|
|
indentLevel++;
|
|||
|
|
}
|
|||
|
|
// 特殊处理else、catch等关键字,它们应该与前面的右大括号在同一行
|
|||
|
|
if (trimmedLine.startsWith('else') || trimmedLine.startsWith('catch') || trimmedLine.startsWith('elif')) {
|
|||
|
|
if (formattedLines.length > 1) {
|
|||
|
|
const prevLine = formattedLines[formattedLines.length - 2];
|
|||
|
|
if (prevLine.trim().endsWith('}')) {
|
|||
|
|
// 将else等关键字与前面的右大括号放在同一行
|
|||
|
|
formattedLines[formattedLines.length - 2] = prevLine.trim() + ' ' + trimmedLine;
|
|||
|
|
formattedLines.pop(); // 移除当前行
|
|||
|
|
indentLevel--; // 修正缩进级别
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 规范化空格(在操作符周围添加适当空格)
|
|||
|
|
return formattedLines.map(line => this.normalizeSpaces(line));
|
|||
|
|
}
|
|||
|
|
// 在操作符周围添加适当空格
|
|||
|
|
normalizeSpaces(line) {
|
|||
|
|
// 在常见的操作符周围添加空格
|
|||
|
|
return line
|
|||
|
|
.replace(/([^\s])(==|!=|<=|>=|<|>|=|\+|-|\*|\/|%)([^\s])/g, '$1 $2 $3')
|
|||
|
|
.replace(/([^\s])(,)([^\s])/g, '$1$2 $3')
|
|||
|
|
.replace(/\s+/g, ' ') // 将多个空格替换为单个空格
|
|||
|
|
.trim();
|
|||
|
|
}
|
|||
|
|
// 创建指定级别的缩进
|
|||
|
|
createIndent(level, options) {
|
|||
|
|
if (options.insertSpaces) {
|
|||
|
|
return ' '.repeat(level * options.tabSize);
|
|||
|
|
}
|
|||
|
|
else {
|
|||
|
|
return '\t'.repeat(level);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
exports.DocumentFormattingProvider = DocumentFormattingProvider;
|
|||
|
|
//# sourceMappingURL=documentFormattingProvider.js.map
|