Skip to content

Commit b87e2dd

Browse files
Gerrnperlxsro
authored andcommitted
feat(formatting): Add configuration for formatting
- 添加了格式化的配置及相关nls - 加载配置
1 parent de37c1f commit b87e2dd

File tree

4 files changed

+144
-16
lines changed

4 files changed

+144
-16
lines changed

masm-tasm/package.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,82 @@
265265
"default": true,
266266
"description": "%config.hover%"
267267
},
268+
"masmtasm.language.Format.align": {
269+
"type": "string",
270+
"default": "segment",
271+
"markdownDescription": "%config.format.align.description%",
272+
"enum": [ "indent", "label", "segment"],
273+
"enumItemLabels": [
274+
"%config.format.align.enum.indent.label%",
275+
"%config.format.align.enum.label.label%",
276+
"%config.format.align.enum.segment.label%"
277+
],
278+
"markdownEnumDescriptions": [
279+
"%config.format.align.enum.indent.md%",
280+
"%config.format.align.enum.label.md%",
281+
"%config.format.align.enum.segment.md%"
282+
]
283+
},
284+
"masmtasm.language.Format.casing": {
285+
"type": "object",
286+
"markdownDescription": "%config.format.casing.description%",
287+
"additionalProperties": {
288+
"type": "string"
289+
},
290+
"properties": {
291+
"instruction": {
292+
"type": "string",
293+
"description": "%config.format.casing.instruction%",
294+
"enum": ["lower", "upper", "title", "off"],
295+
"default": "off"
296+
},
297+
"register": {
298+
"type": "string",
299+
"description": "%config.format.casing.register%",
300+
"enum": ["lower", "upper", "title", "off"],
301+
"default": "off"
302+
},
303+
"directive": {
304+
"type": "string",
305+
"description": "%config.format.casing.directive%",
306+
"enum": ["lower", "upper", "title", "off"],
307+
"default": "off"
308+
},
309+
"operator": {
310+
"type": "string",
311+
"description": "%config.format.casing.operator%",
312+
"enum": ["lower", "upper", "title", "off"],
313+
"default": "off"
314+
}
315+
}
316+
},
317+
"masmtasm.language.Format.alignOperand": {
318+
"type": "boolean",
319+
"default": true,
320+
"markdownDescription": "%config.format.alignOperand.description%"
321+
},
322+
"masmtasm.language.Format.alignTrailingComment": {
323+
"type": "boolean",
324+
"default": true,
325+
"markdownDescription": "%config.format.alignTrailingComment.description%"
326+
},
327+
"masmtasm.language.Format.alignSingleLineComment": {
328+
"type": "boolean",
329+
"default": true,
330+
"markdownDescription": "%config.format.alignSingleLineComment.description%"
331+
},
332+
"masmtasm.language.Format.spaceAfterComma": {
333+
"type": "string",
334+
"default": "off",
335+
"markdownDescription": "%config.format.spaceAfterComma.description%",
336+
"enum": ["always", "never", "off"],
337+
"enumItemLabels": [
338+
"%config.format.spaceAfterComma.enum.always%",
339+
"%config.format.spaceAfterComma.enum.never%",
340+
"%config.format.spaceAfterComma.enum.off%"
341+
]
342+
},
343+
268344
"masmtasm.cpp-docs.links": {
269345
"type": "array",
270346
"items": {

masm-tasm/package.nls.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,24 @@
2121
"config.boxXconfig.description": "configuration for DOSBox-X, use format like the default value, see [dosbox-x](https://dosbox-x.com/wiki/), Please don't set `autoexec `here",
2222
"config.hover": "Display Hover information or not, restart VSCode to apply",
2323
"config.cpp-docs.links": "Links for fetch hover data from [cppdocs](https://github.com/MicrosoftDocs/cpp-docs). \"{vid}\" will be replaced as `Visual Studio Code Language ID`,\"{mid}\" will be replaced as `MLCP language code`, see [vscode-loc](https://github.com/microsoft/vscode-loc)",
24-
"config.PLF": "Experimental programmatic language features like outline,jump to definition/reference. Restart needed"
24+
"config.PLF": "Experimental programmatic language features like outline,jump to definition/reference. Restart needed",
25+
"config.format.align.description": "Align code in different ways",
26+
"config.format.align.enum.indent.label": "Indent only",
27+
"config.format.align.enum.label.label": "Align to label",
28+
"config.format.align.enum.segment.label": "Align within segment",
29+
"config.format.align.enum.indent.md": "Indent the code with fixed size.\n\n```asm\nL1:\n mov ax, 1\n mov bx, 2\nLabel2:\n mov cx, 3\n mov dx, 4\n```",
30+
"config.format.align.enum.label.md": "Align the code to its label\n\n```asm\nL1:\n mov ax, 1\n mov bx, 2\nLabel2:\n mov cx, 3\n mov dx, 4\n```",
31+
"config.format.align.enum.segment.md": "Align the code within segment\n\n```asm\nL1:\n mov ax, 1\n mov bx, 2\nLabel2:\n mov cx, 3\n mov dx, 4\n```",
32+
"config.format.casing.description": "Format the casing of identifiers\n\n`lower` - all lower case\n\n`upper` - all upper case\n\n`title` - title case\n\n`off` - do not change the casing",
33+
"config.format.casing.instruction": "Instructions, e.g. `MOV` `JMP` `PUSH`",
34+
"config.format.casing.directive": "Directives, e.g. `INCLUDE` `END` `PROC`",
35+
"config.format.casing.register": "Registers, e.g. `AX` `SI` `CS`",
36+
"config.format.casing.operator": "Operators, e.g. `HIGH` `LOW` `PTR`",
37+
"config.format.alignOperand.description": "Align operands",
38+
"config.format.alignTrailingComment.description": "Align trailing comments",
39+
"config.format.alignSingleLineComment.description": "Align single line comments",
40+
"config.format.spaceAfterComma.description": "Add space after comma",
41+
"config.format.spaceAfterComma.enum.always": "Always keep a space after comma",
42+
"config.format.spaceAfterComma.enum.never": "Remove space after comma",
43+
"config.format.spaceAfterComma.enum.off": "Do not change the space after comma"
2544
}

masm-tasm/package.nls.zh-cn.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,24 @@
2121
"config.boxXconfig.description": "dosbox-x的配置信息,将会写到插件专用的配置文件中, 参见 [dosbox-x官网的说明](https://dosbox-x.com/wiki/),请勿设置`autoexec`",
2222
"config.hover": "是否显示悬浮提示(hover),可能需要重启vscode来应用变更",
2323
"config.cpp-docs.links": "用于获取[cppdocs](https://github.com/MicrosoftDocs/cpp-docs)信息的链接, \"{vid}\"将被替换为`Visual Studio Code Language ID`,\"{mid}\" 将被替换为`MLCP language code`, 参见 [vscode-loc](https://github.com/microsoft/vscode-loc)",
24-
"config.PLF": "使用提供大纲,跳到定义,查找引用等programmatic features"
24+
"config.PLF": "使用提供大纲,跳到定义,查找引用等programmatic features",
25+
"config.format.align.description": "以不同方式对齐代码",
26+
"config.format.align.enum.indent.label": "仅缩进",
27+
"config.format.align.enum.label.label": "对齐到标签",
28+
"config.format.align.enum.segment.label": "对齐到段",
29+
"config.format.align.enum.indent.md": "以固定的长度缩进代码\n\n```asm\nL1:\n mov ax, 1\n mov bx, 2\nLabel2:\n mov cx, 3\n mov dx, 4\n```",
30+
"config.format.align.enum.label.md": "代码对齐到其所在的标签\n\n```asm\nL1:\n mov ax, 1\n mov bx, 2\nLabel2:\n mov cx, 3\n mov dx, 4\n```",
31+
"config.format.align.enum.segment.md": "代码在段中对齐\n\n```asm\nL1:\n mov ax, 1\n mov bx, 2\nLabel2:\n mov cx, 3\n mov dx, 4\n```",
32+
"config.format.casing.description": "格式化标识符的大小写\n\n`lower` - 全部小写\n\n`upper` - 全部大写\n\n`title` - 首字母大写\n\n`off` - 不改变大小写",
33+
"config.format.casing.instruction": "指令, 例如`MOV` `JMP` `PUSH`",
34+
"config.format.casing.directive": "指令, 例如`INCLUDE` `END` `PROC`",
35+
"config.format.casing.register": "寄存器, 例如`AX` `SI` `CS`",
36+
"config.format.casing.operator": "运算符, 例如`HIGH` `LOW` `PTR`",
37+
"config.format.alignOperand.description": "是否对齐操作数",
38+
"config.format.alignTrailingComment.description": "是否对齐行末注释",
39+
"config.format.alignSingleLineComment.description": "是否对齐单行注释",
40+
"config.format.spaceAfterComma.description": "是否在逗号后添加空格",
41+
"config.format.spaceAfterComma.enum.always": "总是在逗号后保持一个空格",
42+
"config.format.spaceAfterComma.enum.never": "删除逗号后的空格",
43+
"config.format.spaceAfterComma.enum.off": "不改变逗号后的空格"
2544
}

masm-tasm/src/language/AsmDocumentFormattingEdit.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,14 @@ interface FormatConfig {
5858
}
5959

6060

61-
6261
//TODO: offer different operation for different vscode.FormattingOptions
6362
export class AsmDocFormat implements vscode.DocumentFormattingEditProvider {
6463
provideDocumentFormattingEdits(
6564
document: vscode.TextDocument,
6665
options: vscode.FormattingOptions,
6766
token: vscode.CancellationToken
6867
): vscode.TextEdit[] {
69-
const config: FormatConfig = {
70-
tab: !options.insertSpaces,
71-
tabSize: options.tabSize,
72-
align: "label",
73-
instructionCase: "title",
74-
registerCase: "upper",
75-
directiveCase: "lower",
76-
operatorCase: "lower",
77-
alignOperand: true,
78-
alignTrailingComment: true,
79-
alignSingleLineComment: true,
80-
spaceAfterComma: "always",
81-
};
68+
const config = loadFormatConfig(options);
8269
const textedits: vscode.TextEdit[] = [];
8370
const docinfo = DocInfo.getDocInfo(document);
8471
if (docinfo.tree) {
@@ -105,6 +92,33 @@ export class AsmDocFormat implements vscode.DocumentFormattingEditProvider {
10592
}
10693
}
10794

95+
function loadFormatConfig(options: vscode.FormattingOptions): FormatConfig {
96+
const config = vscode.workspace.getConfiguration("masmtasm.language.Format");
97+
98+
const tab = !options.insertSpaces;
99+
const tabSize = options.tabSize;
100+
const casing = config.get<{
101+
instruction: caseType,
102+
register: caseType,
103+
directive: caseType,
104+
operator: caseType,
105+
}>('casing');
106+
return {
107+
tab,
108+
tabSize,
109+
align: config.get<'indent' | 'label' | 'segment'>('align') ?? 'segment',
110+
instructionCase: casing?.instruction ?? 'off',
111+
registerCase: casing?.register ?? 'off',
112+
directiveCase: casing?.directive ?? 'off',
113+
operatorCase: casing?.operator ?? 'off',
114+
alignOperand: config.get<boolean>('alignOperand') ?? true,
115+
alignTrailingComment: config.get<boolean>('alignTrailingComment') ?? true,
116+
alignSingleLineComment: config.get<boolean>('alignSingleLineComment') ?? true,
117+
spaceAfterComma: config.get<'always' | 'never' | 'off'>('spaceAfterComma') ?? 'off',
118+
};
119+
120+
}
121+
108122
function postFormat(text: string[], config: FormatConfig) {
109123
for (let i = text.length - 1; i >= 0; i--) {
110124
// test if current line is a a comment only line, use regexp

0 commit comments

Comments
 (0)