Skip to content

Commit 6661d97

Browse files
committed
基本实现codeformate
1 parent e51f8cc commit 6661d97

File tree

6 files changed

+87
-57
lines changed

6 files changed

+87
-57
lines changed

.vscode/launch.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
],
1919
"preLaunchTask": "${defaultBuildTask}"
2020
},
21+
{
22+
"name": "Run Extension(no i18n)",
23+
"type": "extensionHost",
24+
"request": "launch",
25+
"runtimeExecutable": "${execPath}",
26+
"args": [
27+
"--extensionDevelopmentPath=${workspaceFolder}"
28+
],
29+
"outFiles": [
30+
"${workspaceFolder}/out/**/*.js"
31+
],
32+
"preLaunchTask": "${defaultBuildTask}"
33+
},
2134
{
2235
"name": "Extension Tests",
2336
"type": "extensionHost",

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ All notable changes to the "masm-tasm" extension will be documented in this file
88

99
- [x] `dosbox here`增加了一个命令以在当前文件所在目录下打开dosbox,来提供更加自由的操作,见demo
1010
- [x] 优化diagnose对fatal类错误的匹配,优化大纲视图
11+
- [x] 提供代码格式化功能(需要完善现在的效果并不是很好)
12+
- [x] 提供查找引用功能(目前也并不完美)
1113

14+
鉴于这些功能不完善,你可以在设置中关闭他们(关闭后需要手动重启)
1215
DEMO `dosbox here`示例: 代码来自[dpisdaniel/assembly-pacman](https://github.com/dpisdaniel/assembly-pacman)
1316

1417
![demo pacman](https://github.com/xsro/masm-tasm/raw/next/pics/demo_pacman.gif)

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文](https://github.com/xsro/masm-tasm/blob/master/doc/README_zh.md)|[English](https://github.com/xsro/masm-tasm/blob/master/README.md)
44

5-
Language support for DOS assembly,suitable for studying MASM/TASM and the course <*principles& peripheral technology of microprocessor*>.
5+
Language support for DOS assembly,maybe suitable for studying MASM/TASM and the course <*principles& peripheral technology of microprocessor*>.
66

77
- Offer *grammar*,basic *outline* view and *hover* support for DOS assembly language
88
- Support both **TASM and MASM** assembler tools: choose MASM or TASM in the preference
@@ -67,7 +67,12 @@ the extension has built in tools for windows. But if you need to use assembler o
6767
3. `dosbox`: dosbox.exe and related files
6868
4. `player`: msdos.exe
6969

70-
Linux and other OS user do not need folder dosbox and player. Instead, we should make sure DOSBox can be opened by shell command `dosbox` (node: child_process.exec("dosbox"))
70+
Linux and other OS user do not need folder dosbox and player. Instead, we should make sure DOSBox can be opened by shell command `dosbox` (node: child_process.exec("dosbox")). Use command like
71+
72+
```sh
73+
sudo apt install dosbox #install dosbox
74+
dosbox #if successfully opened the dosbox, this means the extension can work
75+
```
7176

7277
### About use of DOSBox
7378

src/language/provider.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class AsmHoverProvider implements vscode.HoverProvider {
55
async provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken) {
66
let output: vscode.MarkdownString = new vscode.MarkdownString()
77
let range = document.getWordRangeAtPosition(new vscode.Position(position.line, position.character));
8+
info.scanDocumnt(document)//scan thdocumente
89
if (range) {
910
let wordo = document.getText(range)
1011
let word = wordo.toLowerCase()
@@ -31,6 +32,7 @@ class AsmDefProvider implements vscode.DefinitionProvider {
3132
async provideDefinition(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken) {
3233
let output: vscode.Location | undefined
3334
let range = document.getWordRangeAtPosition(new vscode.Position(position.line, position.character));
35+
info.scanDocumnt(document)//scan thdocumente
3436
if (range) {
3537
let wordo = document.getText(range)
3638
let tasmsymbol = info.findSymbol(wordo)
@@ -44,7 +46,7 @@ class AsmDefProvider implements vscode.DefinitionProvider {
4446
class Asmsymbolprovider implements vscode.DocumentSymbolProvider {
4547
provideDocumentSymbols(document: vscode.TextDocument, token: vscode.CancellationToken) {
4648
let docsymbol: vscode.DocumentSymbol[] = []
47-
docsymbol = info.getVscSymbols(document)
49+
docsymbol = info.scanDocumnt(document)
4850
return docsymbol
4951
}
5052

@@ -54,6 +56,7 @@ class AsmReferenceProvider implements vscode.ReferenceProvider {
5456
provideReferences(document: vscode.TextDocument, position: vscode.Position, context: vscode.ReferenceContext, token: vscode.CancellationToken) {
5557
let range = document.getWordRangeAtPosition(new vscode.Position(position.line, position.character))
5658
let output: vscode.Location[] = []
59+
info.scanDocumnt(document)//scan thdocumente
5760
if (range) {
5861
let word = document.getText(range)
5962
output = info.getrefer(word, document)
@@ -62,8 +65,8 @@ class AsmReferenceProvider implements vscode.ReferenceProvider {
6265
}
6366
}
6467
class AsmDocFormat implements vscode.DocumentFormattingEditProvider {
65-
provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken):vscode.TextEdit[] {
66-
info.getVscSymbols(document)//scan the document
68+
provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken): vscode.TextEdit[] {
69+
info.scanDocumnt(document)//scan the document
6770
return info.codeformatting(document, options)
6871
}
6972
}

src/language/wordinfo.ts

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as vscode from 'vscode';
22
import * as nls from 'vscode-nls'
33
const localize = nls.loadMessageBundle()
44
let symbols: TasmSymbol[] = []//record symbols in the doc in a simple way
5-
let _document: vscode.TextDocument//record the processing document
5+
let _documentText: string | undefined//record the processing document
6+
let _docUri: vscode.Uri | undefined
67
let asmline: Asmline[] = []//split and analyse the document
78
let docsymbol: vscode.DocumentSymbol[] = []//record symbols in the doc in a VSCode way
89

@@ -18,7 +19,6 @@ export function getrefer(word: string, doc: vscode.TextDocument): vscode.Locatio
1819
case linetype.macro: skip = true; break
1920
case linetype.endm: skip = false; break
2021
case linetype.label:
21-
case linetype.labelB:
2222
if (skip === false) {
2323
//TODO:优化匹配方式,对于变量应该考虑多种复杂的表达式如:查找var不能找到nvar
2424
if (def?.type === symboltype.variable && item.operand?.includes(word)) {
@@ -33,7 +33,7 @@ export function getrefer(word: string, doc: vscode.TextDocument): vscode.Locatio
3333
let start = item.str.indexOf(word)
3434
r = new vscode.Range(index, start, index, start + word.length)
3535
}
36-
if (r) output.push(new vscode.Location(_document.uri, r))
36+
if (r) output.push(new vscode.Location(doc.uri, r))
3737
}
3838
else {
3939

@@ -45,45 +45,45 @@ export function getrefer(word: string, doc: vscode.TextDocument): vscode.Locatio
4545
}
4646
return output
4747
}
48-
export function codeformatting(document: vscode.TextDocument, options: vscode.FormattingOptions):vscode.TextEdit[] {
49-
let formator:vscode.TextEdit[] =[],
50-
namesize:number=0,optsize:number=0,oprsize:number=0,str:string|undefined=undefined,
51-
r:vscode.Range,Endline: string = '\r\n'
48+
export function codeformatting(document: vscode.TextDocument, options: vscode.FormattingOptions): vscode.TextEdit[] {
49+
let formator: vscode.TextEdit[] = [],
50+
namesize: number = 0, optsize: number = 0, oprsize: number = 0, str: string | undefined = undefined,
51+
r: vscode.Range, Endline: string = '\r\n'
5252
if (document.eol === vscode.EndOfLine.LF) Endline = '\n'
5353
//scan the asmlines for information
5454
asmline.forEach(
55-
(item)=>{
56-
if(item.name) namesize=item.name.length>optsize?item.name.length:namesize
57-
if(item.operator) optsize=item.operator.length>optsize?item.operator.length:optsize
58-
if(item.operand) optsize=item.operand.length>optsize?item.operand.length:optsize
59-
console.log(optsize)
55+
(item) => {
56+
if (item.name) namesize = item.name.length > namesize ? item.name.length : namesize
57+
if (item.operator) optsize = item.operator.length > optsize ? item.operator.length : optsize
58+
if (item.operand) oprsize = item.operand.length > oprsize ? item.operand.length : oprsize
59+
//console.log(item.operator,optsize)
6060
}
6161
)
6262
asmline.forEach(
63-
(item)=>{
64-
if(item.operator){
65-
str="\t"
66-
let length:number=0
67-
if(item.name?.length) length=item.name.length
68-
for(let i=0;i<namesize-length;i++) str+=" "//标签变量名前补充空格
69-
if(item.type===linetype.label && item.name) str+=item.name+":"
70-
else if(item.type===linetype.variable && item.name) str+=item.name+" "
71-
else str+=" "
72-
if(item.name?.length) length=item.operator.length
73-
else length=0
74-
str+=item.operator
63+
(item) => {
64+
if (item.type === linetype.label || item.type === linetype.variable) {
65+
str = "\t"
66+
let length: number = 0
67+
if (item.name?.length) length = item.name.length
68+
if (item.type === linetype.label && item.name) str += item.name + ":"
69+
else if (item.type === linetype.variable && item.name) str += item.name + " "
70+
else str += " "
71+
for (let i = 0; i < namesize - length; i++) str += " "//标签变量名前补充空格
7572

76-
for(let i=0;i<optsize-length;i++) str+=" "//操作码后补充空格
77-
str+=" "+item.operand
78-
if(item.comment)str+=item.comment
73+
str += item.operator
74+
// if(item.name?.length) length=item.operator.length
75+
// else length=0
76+
//for(let i=0;i<optsize-length;i++) str+=" "//操作码后补充空格
77+
str += "\t" + item.operand
78+
if (item.comment) str += "\t" + item.comment
79+
}
80+
else {
81+
str = item.str.replace(/\s+/, " ")
7982
}
80-
else{
81-
str=item.str.replace(/\s+/," ")
83+
if (str && str !== item.str) {
84+
r = new vscode.Range(item.line, 0, item.line, item.str.length)
85+
formator.push(vscode.TextEdit.replace(document.validateRange(r), str))
8286
}
83-
if(str && str!==item.str){
84-
r=new vscode.Range(item.line,0,item.line,item.str.length)
85-
formator.push(vscode.TextEdit.replace(document.validateRange(r),str))
86-
}
8787
}
8888
)
8989

@@ -120,7 +120,7 @@ class TasmSymbol {
120120
constructor(type: number, name: string, RangeorPosition: vscode.Range | vscode.Position) {
121121
this.type = type
122122
this.name = name
123-
if (_document) this.location = new vscode.Location(_document.uri, RangeorPosition)
123+
if (_docUri) this.location = new vscode.Location(_docUri, RangeorPosition)
124124
}
125125
public markdown(): vscode.MarkdownString {
126126
let md = new vscode.MarkdownString()
@@ -231,34 +231,32 @@ class Asmline {
231231
return flag
232232
}
233233
private getvarlabel(item: string) {
234-
let r = item.match(/^\s*(\w+\s*:|)\s*(\w+)\s+(.*)$/)
234+
let r = item.match(/^\s*(\w+\s*:|)\s*(\w+|)(\s+.*|)$/)
235235
let name: string | undefined
236236
if (r) {
237237
name = r[1]
238-
if (name.length === 0) this.type = linetype.labelB
239-
else {
238+
this.type = linetype.label
239+
if (name.length !== 0) {
240240
this.name = name.slice(0, name.length - 1).trim()
241-
this.type = linetype.label
242241
let start = item.indexOf(r[1])
243242
let one: TasmSymbol = new TasmSymbol(symboltype.label, this.name, new vscode.Position(this.line, start))
244243
symbols.push(one)
245244
}
246245
this.operator = r[2]
247-
this.operand = r[3]
246+
this.operand = r[3].trim()
248247
}
249-
r = item.match(/^\s*(\w+\s+|)([dD][bBwWdDfFqQtT]|=|EQU|equ)\s+(.*)$/)
248+
r = item.match(/^\s*(\w+\s+|)([dD][bBwWdDfFqQtT]|=|EQU|equ)(\s+.*)$/)
250249
if (r) {
251250
name = r[1].trim()
252-
if (name.length === 0) this.type = linetype.variableB
253-
else {
254-
this.type = linetype.variable
251+
this.type = linetype.variable
252+
if (name.length !== 0) {
255253
this.name = name
256254
let start = item.indexOf(r[1])
257255
let one: TasmSymbol = new TasmSymbol(symboltype.variable, name, new vscode.Position(this.line, start))
258256
symbols.push(one)
259257
}
260258
this.operator = r[2]
261-
this.operand = r[3]
259+
this.operand = r[3].trim()
262260
}
263261
}
264262
public varlabelsymbol(): vscode.DocumentSymbol | undefined {
@@ -287,10 +285,11 @@ class Asmline {
287285
}
288286

289287
function sacnDoc(document: vscode.TextDocument) {
290-
_document = document; symbols = []; asmline = []
288+
_documentText = document.getText(); _docUri = document.uri
289+
symbols = []; asmline = []
291290
let splitor: string = '\r\n'
292291
if (document.eol === vscode.EndOfLine.LF) splitor = '\n'
293-
let doc = document.getText().split(splitor)
292+
let doc = _documentText.split(splitor)
294293
// scan the document for necessary information
295294
let docsymbol: vscode.DocumentSymbol[] = []
296295
doc.forEach(
@@ -300,10 +299,9 @@ function sacnDoc(document: vscode.TextDocument) {
300299
)
301300
console.log(asmline)
302301
}
303-
export function getVscSymbols(doc?: vscode.TextDocument): vscode.DocumentSymbol[] {
304-
docsymbol = []
305-
if (doc && doc !== _document) sacnDoc(doc)
302+
function symboltree() {
306303
let i: number
304+
//寻找段,宏指令信息
307305
asmline.forEach(
308306
(line, index, array) => {
309307
//是否为宏指令
@@ -395,6 +393,14 @@ export function getVscSymbols(doc?: vscode.TextDocument): vscode.DocumentSymbol[
395393
}
396394
}
397395
)
396+
397+
}
398+
export function scanDocumnt(doc?: vscode.TextDocument): vscode.DocumentSymbol[] {
399+
docsymbol = []
400+
if (doc && (doc.getText() !== _documentText || doc.uri !== _docUri)) {
401+
sacnDoc(doc)
402+
symboltree()
403+
}
398404
return docsymbol
399405
}
400406

syntaxes/assembly.tmLanguage.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"keywords": {
1414
"patterns": [{
1515
"name": "keyword.assembly",
16-
"match": "\\b((?i)(hlt|nop|xadd|xchg|cmpxchg|ror|rol|rcl|rcr|into|iret|loopne|loopnz|enter|leave|shrd|shld|fwait|wait|xlat|les|xchg|rep|mod|movs|lea|str|req|push|call|mov|ret|pop|int|loop|loope|loopz|lea|add|sub|mul|neg|div|idiv|imul|pusha|popa|inc|dec|jmp|jle|jge|jg|jl|jle|jc|jnc|ja|jna|jz|jnz|je|jne|js|jns|jp|jnp|jo|jno|cmp|and|or|xor|not|ja|jb|jae|jbe|jcxz|in|out|shl|shr))\\b"
16+
"match": "\\b((?i)(hlt|nop|xadd|xchg|cmpxchg|sal|ror|rol|rcl|rcr|into|iret|loopne|loopnz|enter|leave|shrd|shld|fwait|wait|xlat|les|xchg|rep|mod|movs|lea|str|req|push|call|mov|ret|pop|int|loop|loope|loopz|lea|add|sub|mul|neg|div|idiv|imul|pusha|popa|inc|dec|jmp|jle|jge|jg|jl|jle|jc|jnc|ja|jna|jz|jnz|je|jne|js|jns|jp|jnp|jo|jno|cmp|and|or|xor|not|ja|jb|jae|jbe|jcxz|in|out|shl|shr))\\b"
1717
},
1818
{
1919
"name":"keyword.control.flags.assmebly",
@@ -48,8 +48,8 @@
4848
"match": "\\b((?i)(CODESEG|DATASEG|IDEAL|MODEL|STACK|width|length|ge|le|this|times|far|near)\\b|@d)ata"
4949
},
5050
{
51-
"name":"keyword.segment.name",
52-
"match": "\\b((?i)(segment|ends))\\b"
51+
"name":"keyword.segment.assmebly",
52+
"match": "\\b((?i)(segment|ends|assume))\\b"
5353
},
5454
{
5555
"name":"keyword.less.preCompiled.assembly",

0 commit comments

Comments
 (0)