1.0.1 代码格式化移植新版

This commit is contained in:
睿 安
2025-09-17 11:28:54 +08:00
parent dc0cbe71dc
commit 6e550f1112
15 changed files with 818 additions and 344 deletions

View File

@@ -35,82 +35,39 @@ var __importStar = (this && this.__importStar) || (function () {
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocumentFormattingProvider = void 0;
const vscode = __importStar(require("vscode"));
// 文档格式化提供者类
const beautifyHelper_1 = require("../utils/beautifyHelper");
/**
* 文档格式化提供者类 - 使用 js-beautify.js
*/
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);
try {
// 获取文档全文
const fullText = document.getText();
// 检查是否是 Squirrel 代码
if (!(0, beautifyHelper_1.isSquirrelCode)(fullText)) {
return edits; // 如果不是 Squirrel 代码,不应用格式化
}
// 使用 js-beautify 格式化代码
const formattedText = (0, beautifyHelper_1.beautifyCode)(fullText, options);
// 如果格式化后的代码与原代码相同,不需要修改
if (formattedText === fullText) {
return edits;
}
// 创建编辑操作,替换整个文档
const fullRange = new vscode.Range(document.positionAt(0), document.positionAt(fullText.length));
edits.push(vscode.TextEdit.replace(fullRange, formattedText));
}
catch (error) {
console.error('文档格式化失败:', error);
// 如果格式化失败,返回空编辑数组,保持原代码不变
}
// 格式化文档
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