Skip to content

Commit 5006d0b

Browse files
committed
🐛 fix #31 Format Document
1 parent d608127 commit 5006d0b

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed
Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,52 @@
11
import * as vscode from 'vscode';
2+
import { eolString } from '../utils/eol';
23
import { DocInfo, linetype, Asmline } from "./scanDoc";
4+
35
//TODO: offer different operation for different vscode.FormattingOptions
46
export class AsmDocFormat implements vscode.DocumentFormattingEditProvider {
5-
provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] {
7+
provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): vscode.TextEdit[] {
68
const textedits: vscode.TextEdit[] = [];
9+
const tabString = options.insertSpaces ? new Array(options.tabSize).fill(" ").join("") : "\t";
710
const docinfo = DocInfo.getDocInfo(document);
811
if (docinfo.tree) {
9-
docinfo.tree.forEach(
10-
(item) => {
11-
formateline(item.range.start.line, item.range.end.line, docinfo.lines, document, textedits);
12+
for (const item of docinfo.tree) {
13+
if (token.isCancellationRequested) {
14+
return textedits;
1215
}
13-
);
16+
const newText = formateline(item.range, docinfo.lines, tabString);
17+
const range = document.validateRange(item.range);
18+
textedits.push(new vscode.TextEdit(range, newText.join(eolString(document.eol))));
19+
}
1420
}
1521
return textedits;
1622
}
1723
}
18-
function formateline(beg: number, end: number, asmline: Asmline[], document: vscode.TextDocument, formator: vscode.TextEdit[]): vscode.TextEdit[] {
24+
25+
/**
26+
* format a segment of assembly code
27+
* @param range the range of the code
28+
* @param asmline the array of lines information
29+
* @param tabString the string to used as tab
30+
* @returns
31+
*/
32+
function formateline(range: vscode.Range, asmline: Asmline[], tabString = "\t"): string[] {
1933
let namesize = 0, optsize = 0, oprsize = 0,
20-
str: string | undefined = undefined, r: vscode.Range, i: number;
34+
str: string | undefined = undefined;
35+
const output: string[] = [];
36+
2137
//scan the asmlines for information
22-
for (i = beg; i < end; i++) {
38+
for (let i = range.start.line; i <= range.end.line; i++) {
2339
const item = asmline[i];
2440
if (item.name) { namesize = item.name.length > namesize ? item.name.length : namesize; }//find the maxlength of label name or variabel name
2541
if (item.operator) { optsize = item.operator.length > optsize ? item.operator.length : optsize; }//find the maxlength of operator
2642
if (item.operand) { oprsize = item.operand.length > oprsize ? item.operand.length : oprsize; }//find the maxlength of operand
2743
}
28-
for (i = beg; i < end; i++) {
44+
45+
for (let i = range.start.line; i <= range.end.line; i++) {
2946
str = undefined;
3047
const item = asmline[i];
3148
if (item.type === linetype.label || item.type === linetype.variable) {
32-
str = "\t";
49+
str = tabString;
3350
let length = 0;
3451
if (item.name?.length) { length = item.name.length; }
3552
if (item.type === linetype.label && item.name) { str += item.name + ":"; }
@@ -47,25 +64,22 @@ function formateline(beg: number, end: number, asmline: Asmline[], document: vsc
4764
if (item.operand?.length) { length = item.operand.length; }
4865
else { length = 0; }
4966
for (let i = 0; i < oprsize - length; i++) { str += " "; }//操作数后补充空格
50-
str += "\t" + item.comment;
67+
str += tabString + item.comment;
5168
}
5269
}
5370
else if (item.type === linetype.onlycomment) {
54-
str = "\t" + item.comment;
71+
str = tabString + item.comment;
5572
}
5673
else if (item.main) {
5774
str = item.main.replace(/\s+/, " ");
5875
const length: number = namesize + 1 + optsize + 1 + oprsize - str.length;
5976
if (item.comment) {
6077
for (let i = 0; i < length; i++) { str += " "; }//后补充空格
61-
str += "\t\t" + item.comment;
78+
str += tabString + tabString + item.comment;
6279
}
6380
}
64-
if (str && str !== item.str) {
65-
r = new vscode.Range(item.line, 0, item.line, item.str.length);
66-
formator.push(vscode.TextEdit.replace(document.validateRange(r), str));
67-
}
81+
output.push(str && str !== item.str ? str : item.str);
6882
}
69-
return formator;
83+
return output;
7084
}
7185

src/language/scanDoc.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ function lines2tree(asmlines: Asmline[]): SYMBOLINFO {
276276
}
277277
//finded the end of macro
278278
if (line.name && lineEndmacro?.line) {
279-
const macrorange = new vscode.Range(line.line, line.index, lineEndmacro?.line, lineEndmacro?.index);
279+
const macrorange = new vscode.Range(line.line, line.index, lineEndmacro?.line, lineEndmacro.str.length);
280280
symbols.push(new AsmSymbol(KeywordType.Macro, line.name, macrorange));
281281
const symbol1 = new vscode.DocumentSymbol(line.name, getType(KeywordType.Macro), SymbolVSCfy(KeywordType.Macro), macrorange, new vscode.Range(line.line, line.index, line.line, line.index + line.name.length));
282282
VSCsymbols.push(symbol1);
@@ -296,7 +296,7 @@ function lines2tree(asmlines: Asmline[]): SYMBOLINFO {
296296
if (array[i].type === linetype.endp && proc?.name === array[i].name) {
297297
const _name = array[i].name;
298298
if (proc?.name && _name) {
299-
const range: vscode.Range = new vscode.Range(proc?.line, proc?.index, array[i].line, array[i].index + _name.length);
299+
const range: vscode.Range = new vscode.Range(proc?.line, proc?.index, array[i].line, array[i].str.length);
300300
const srange: vscode.Range = new vscode.Range(proc.line, proc.index, proc?.line, proc?.index + proc?.name?.length);
301301
procschild.push(new vscode.DocumentSymbol(proc?.name, getType(KeywordType.Procedure), SymbolVSCfy(KeywordType.Procedure), range, srange));
302302
symbols.push(new AsmSymbol(KeywordType.Procedure, _name, range));
@@ -315,7 +315,7 @@ function lines2tree(asmlines: Asmline[]): SYMBOLINFO {
315315
}
316316
//finded the end of segment
317317
if (line.name && lineEndSegment?.line) {
318-
const range = new vscode.Range(line.line, line.index, lineEndSegment?.line, lineEndSegment?.index);
318+
const range = new vscode.Range(line.line, line.index, lineEndSegment?.line, lineEndSegment.str.length);
319319
symbols.push(new AsmSymbol(KeywordType.Segment, line.name, range));
320320
const symbol1 = new vscode.DocumentSymbol(line.name, getType(KeywordType.Segment), SymbolVSCfy(KeywordType.Segment), range, new vscode.Range(line.line, line.line, line.line, line.line + line.name.length));
321321
symbol1.children = procschild;

src/utils/eol.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { EndOfLine } from "vscode";
2+
3+
export function eolString(eol: EndOfLine) {
4+
switch (eol) {
5+
case EndOfLine.CRLF:
6+
return "\r\n";
7+
case EndOfLine.LF:
8+
return "\n";
9+
}
10+
}

0 commit comments

Comments
 (0)