Skip to content

Commit 278a2bb

Browse files
committed
使用textdocument 在各个函数之间交互
1 parent cd5735a commit 278a2bb

File tree

4 files changed

+67
-79
lines changed

4 files changed

+67
-79
lines changed

src/DOSBox.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Uri,workspace, window} from 'vscode'
1+
import { Uri,workspace, window, TextDocument} from 'vscode'
22

33
import { Config } from './configration'
44
import { execSync} from 'child_process'
5-
import { landiagnose } from './diagnose'
5+
import { landiagnose } from './language/diagnose'
66
export class DOSBox{
77
constructor(){
88
}
@@ -12,52 +12,45 @@ export class DOSBox{
1212
* @param fileuri 清理工作文件夹,复制该文件到工作文件夹,假如没有那么就不处理
1313
* @param diag 如果有则诊断输出信息
1414
*/
15-
public openDOSBox(conf:Config,more?:string,fileuri?:Uri,diag?:landiagnose) {
15+
public openDOSBox(conf:Config,more?:string,doc?:TextDocument,diag?:landiagnose) {
1616
let boxcommand=' '
1717
if(more){
1818
let boxparam=more.replace(/\n/g,'"-c "')
1919
boxcommand='-c "'+boxparam+'"'
2020
}
2121
if(process.platform=='win32'){
2222
let wincommand='start/min/wait "" "'+conf.path+'/dosbox/dosbox.exe" -conf "'+conf.dosboxconfuri.fsPath+'" '
23-
if(fileuri) wincommand='del/Q T*.* & copy "'+fileuri.fsPath+'" "T.ASM" & '+wincommand
23+
if(doc) wincommand='del/Q T*.* & copy "'+doc.fileName+'" "T.ASM" & '+wincommand
2424
execSync(wincommand+boxcommand,{cwd:conf.workpath,shell:'cmd.exe'})
2525
console.log(wincommand+boxcommand)
2626
}
2727
else{
2828
let linuxcommand='dosbox -conf "'+conf.dosboxconfuri.fsPath+'" '
29-
if(fileuri) linuxcommand='rm -f [Tt]*.*;cp "'+fileuri.fsPath+'" T.ASM;'+linuxcommand
29+
if(doc) linuxcommand='rm -f [Tt]*.*;cp "'+doc.fileName+'" T.ASM;'+linuxcommand
3030
console.log(linuxcommand+boxcommand)
3131
execSync(linuxcommand+boxcommand,{cwd:conf.workpath})
3232

3333
}
3434
if(diag) this.BOXdiag(conf,diag)
3535
}
3636
private BOXdiag(conf:Config,diag:landiagnose):string{
37-
let info:string=' ',content
37+
let info:string=' ',content:string
3838
let turi=window.activeTextEditor?.document.uri
39+
let document=window.activeTextEditor?.document
3940
let texturi:Uri
40-
if (turi) {
41-
texturi=turi
41+
if (document) {
42+
content=document.getText()
4243
workspace.fs.readFile(conf.workloguri).then(
4344
(text)=>{
4445
info=text.toString()
45-
workspace.fs.readFile(texturi).then(
46-
(text)=>{
47-
content=text.toString()
48-
if(diag.ErrMsgProcess(content,info,texturi,conf.MASMorTASM)==0){
49-
let Errmsgwindow=conf.MASMorTASM+'汇编出错,无法运行/调试'
50-
window.showErrorMessage(Errmsgwindow);
51-
}
52-
}
53-
)
54-
},
46+
if(diag.ErrMsgProcess(content,info,texturi,conf.MASMorTASM)==0){
47+
let Errmsgwindow=conf.MASMorTASM+'汇编出错,无法运行/调试'
48+
window.showErrorMessage(Errmsgwindow);
49+
}},
5550
()=>{console.error('read dosbox mode T.txt FAILED')}
5651
)}
5752
return info
5853
}
59-
60-
6154
}
6255

6356

src/MSDOS-player.ts

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { workspace, window, Terminal,Uri} from 'vscode'
1+
import { workspace, window, Terminal,Uri, TextDocument} from 'vscode'
22
import { Config } from './configration';
33
import { exec } from 'child_process'
44
import { DOSBox } from './DOSBox'
5-
import { landiagnose } from './diagnose'
5+
import { landiagnose } from './language/diagnose'
66
import * as nls from 'vscode-nls';
77
const localize = nls.loadMessageBundle()
88
export class MSDOSplayer{
@@ -18,44 +18,39 @@ export class MSDOSplayer{
1818
* @param diag 处理输出信息的类
1919
* @param fileuri 需要处理的文件的Uri
2020
*/
21-
public PlayerASM(conf:Config,isrun:boolean,viaplayer:boolean,diag:landiagnose,fileuri:Uri)
21+
public PlayerASM(conf:Config,isrun:boolean,viaplayer:boolean,diag:landiagnose,doc:TextDocument)
2222
{
2323
let filecontent:string
24-
if(fileuri){
25-
workspace.fs.readFile(fileuri).then(
26-
(text)=>{
27-
filecontent=text.toString()
28-
}
29-
)
30-
const filename = fileuri.fsPath
31-
let command='"'+conf.msbatpath+'" "'+conf.path+'" '+conf.MASMorTASM+' "'+filename+'" "'+conf.workpath+'"'
32-
console.log(command)
33-
exec(command,{cwd:conf.path,shell:'cmd.exe'},(error, stdout, stderr) =>
24+
filecontent=doc.getText()
25+
const filename = doc.fileName
26+
let command='"'+conf.msbatpath+'" "'+conf.path+'" '+conf.MASMorTASM+' "'+filename+'" "'+conf.workpath+'"'
27+
console.log(command)
28+
exec(command,{cwd:conf.path,shell:'cmd.exe'},(error, stdout, stderr) =>
29+
{
30+
if (error) {console.error(`exec playerasm.bat: ${error}`);}
31+
let code=diag.ErrMsgProcess(filecontent,stdout,doc.uri,conf.MASMorTASM)
32+
switch(code)
3433
{
35-
if (error) {console.error(`exec playerasm.bat: ${error}`);}
36-
let code=diag.ErrMsgProcess(filecontent,stdout,fileuri,conf.MASMorTASM)
37-
switch(code)
38-
{
39-
case 0:
40-
let Errmsgwindow=localize("msdos.error","{0} Error,Can't generate .exe file",conf.MASMorTASM)
41-
window.showErrorMessage(Errmsgwindow);
42-
break
43-
case 1:
44-
let warningmsgwindow=localize("msdos.warn","{0} Warning,successfully generate .exe file,but assembler has some warning message",conf.MASMorTASM);
45-
let Go_on=localize("msdos.continue","continue")
46-
let Stop=localize("msdos.stop","stop")
47-
window.showInformationMessage(warningmsgwindow, Go_on, Stop).then(result => {
48-
if (result === Go_on) {
49-
this.afterlink(conf,viaplayer,isrun)
50-
}
51-
});
52-
break
53-
case 2:
54-
this.afterlink(conf,viaplayer,isrun)
55-
break
56-
}
57-
Config.writefile(Uri.joinPath(conf.toolsUri,'./work/T.TXT'),stdout)
58-
})}
34+
case 0:
35+
let Errmsgwindow=localize("msdos.error","{0} Error,Can't generate .exe file",conf.MASMorTASM)
36+
window.showErrorMessage(Errmsgwindow);
37+
break
38+
case 1:
39+
let warningmsgwindow=localize("msdos.warn","{0} Warning,successfully generate .exe file,but assembler has some warning message",conf.MASMorTASM);
40+
let Go_on=localize("msdos.continue","continue")
41+
let Stop=localize("msdos.stop","stop")
42+
window.showInformationMessage(warningmsgwindow, Go_on, Stop).then(result => {
43+
if (result === Go_on) {
44+
this.afterlink(conf,viaplayer,isrun)
45+
}
46+
});
47+
break
48+
case 2:
49+
this.afterlink(conf,viaplayer,isrun)
50+
break
51+
}
52+
Config.writefile(Uri.joinPath(conf.toolsUri,'./work/T.TXT'),stdout)
53+
})
5954
}
6055
private outTerminal(run:boolean,conf:Config) {
6156
let myenv=process.env

src/runcode.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { MSDOSplayer } from './MSDOS-player'
55
import { Uri } from 'vscode';
66
import * as nls from 'vscode-nls';
77
const localize = nls.loadMessageBundle()
8-
import { landiagnose } from './diagnose';
8+
import { landiagnose } from './language/diagnose';
99
export class runcode{
1010
private readonly extOutChannel: vscode.OutputChannel;
1111
private readonly exturi:Uri
@@ -21,45 +21,45 @@ export class runcode{
2121
this.dosbox=new DOSBox()
2222
this.landiag=new landiagnose(this.extOutChannel)
2323
}
24-
private Openemu(fileuri:Uri){
25-
let openemumsg=localize("openemu.msg","\nMASM/TASM>>Open DOSBox:{0}",fileuri.fsPath)
24+
private Openemu(doc:vscode.TextDocument){
25+
let openemumsg=localize("openemu.msg","\nMASM/TASM>>Open DOSBox:{0}",doc.fileName)
2626
this.extOutChannel.appendLine(openemumsg);
27-
this.dosbox.openDOSBox(this._config,undefined,fileuri,)
27+
this.dosbox.openDOSBox(this._config,undefined,doc)
2828
}
2929
/**运行汇编代码的入口
3030
* 获取拓展的设置,并执行相应操作
3131
*/
32-
private Run(fileuri:Uri){
33-
let runmsg=localize("run.msg","\n{0}({1})>>Run:{2}",this._config.MASMorTASM,this._config.DOSemu,fileuri.fsPath)
32+
private Run(doc:vscode.TextDocument){
33+
let runmsg=localize("run.msg","\n{0}({1})>>Run:{2}",this._config.MASMorTASM,this._config.DOSemu,doc.fileName)
3434
this.extOutChannel.appendLine(runmsg);
3535
switch(this._config.DOSemu){
36-
case 'msdos player': this.msdosplayer.PlayerASM(this._config,true,true,this.landiag,fileuri);break;
36+
case 'msdos player': this.msdosplayer.PlayerASM(this._config,true,true,this.landiag,doc);break;
3737
case 'dosbox':
3838
let text='x:\\boxasm.bat '+this._config.MASMorTASM+' run '+this._config.boxrunbat
39-
this.dosbox.openDOSBox(this._config,text,fileuri,this.landiag)
39+
this.dosbox.openDOSBox(this._config,text,doc,this.landiag)
4040
break;
41-
case 'auto': this.msdosplayer.PlayerASM(this._config,true,false,this.landiag,fileuri);break;
41+
case 'auto': this.msdosplayer.PlayerASM(this._config,true,false,this.landiag,doc);break;
4242
default: throw new Error("未指定emulator");
4343
}
4444
}
4545
/**调试程序
4646
* 获取拓展的设置并执行相应操作
4747
*/
48-
private Debug(fileuri:Uri){
49-
let debugmsg=localize("debug.msg","\n{0}({1})>>Debug:{2}",this._config.MASMorTASM,this._config.DOSemu,fileuri.fsPath)
48+
private Debug(doc:vscode.TextDocument){
49+
let debugmsg=localize("debug.msg","\n{0}({1})>>Debug:{2}",this._config.MASMorTASM,this._config.DOSemu,doc.fileName)
5050
this.extOutChannel.appendLine(debugmsg);
5151
if (this._config.DOSemu=='msdos player' && this._config.MASMorTASM=='MASM'){
52-
this.msdosplayer.PlayerASM(this._config,false,true,this.landiag,fileuri)
52+
this.msdosplayer.PlayerASM(this._config,false,true,this.landiag,doc)
5353
}
5454
else if (this._config.DOSemu=='auto')
5555
{
5656
let inplayer:boolean=false
5757
if (this._config.MASMorTASM=='MASM') inplayer=true
58-
this.msdosplayer.PlayerASM(this._config,false,inplayer,this.landiag,fileuri)
58+
this.msdosplayer.PlayerASM(this._config,false,inplayer,this.landiag,doc)
5959
}
6060
else{
6161
let text='x:\\boxasm.bat '+this._config.MASMorTASM+' debug'
62-
this.dosbox.openDOSBox(this._config,text,fileuri,this.landiag)
62+
this.dosbox.openDOSBox(this._config,text,doc,this.landiag)
6363
}
6464
}
6565
public cleanalldiagnose(){
@@ -73,20 +73,20 @@ export class runcode{
7373
public runcode(command:string){
7474
let exturi=this.exturi
7575
vscode.workspace.onDidChangeConfiguration((event) =>{this._config=new Config(exturi)},this._config)
76-
const fileuri=vscode.window.activeTextEditor?.document.uri
77-
if(fileuri)
76+
let document=vscode.window.activeTextEditor?.document
77+
if(document)
7878
{
7979
if (this._config.savefirst && vscode.window.activeTextEditor?.document.isDirty) {
80-
vscode.window.activeTextEditor?.document.save().then(()=>this.asmit(command,fileuri))
80+
document.save().then(()=>{if(document) this.asmit(command,document)})
8181
}
82-
else this.asmit(command,fileuri)
82+
else this.asmit(command,document)
8383
}
8484
}
85-
private asmit(command:string,fileuri:Uri){
85+
private asmit(command:string,doc:vscode.TextDocument){
8686
switch (command){
87-
case 'opendosbox':this.Openemu(fileuri);break
88-
case 'run':this.Run(fileuri);break
89-
case 'debug':this.Debug(fileuri);break
87+
case 'opendosbox':this.Openemu(doc);break
88+
case 'run':this.Run(doc);break
89+
case 'debug':this.Debug(doc);break
9090
}
9191
}
9292
}

0 commit comments

Comments
 (0)