@@ -3,29 +3,44 @@ import { RequestMessage } from "vscode-languageserver";
33import * as utils from "./utils" ;
44import * as path from "path" ;
55import { exec } from "child_process" ;
6+ import * as tmp from "tmp" ;
7+ import fs from "fs" ;
8+
9+ let findExecutable = ( uri : string ) => {
10+ let filePath = fileURLToPath ( uri ) ;
11+ let projectRootPath = utils . findProjectRootOfFile ( filePath ) ;
12+ if ( projectRootPath == null ) {
13+ return null ;
14+ } else {
15+ // Currently assumes the dump command is "bin.exe" at the project root.
16+ let binaryPath = path . join ( projectRootPath , "bin.exe" ) ;
17+ if ( fs . existsSync ( binaryPath ) ) {
18+ return { binaryPath, filePath, cwd : projectRootPath } ;
19+ } else {
20+ return null ;
21+ }
22+ }
23+ } ;
624
725export function runDumpCommand (
8- msg : RequestMessage ,
26+ msg : RequestMessage ,
927 onResult : (
1028 result : { hover ?: string ; definition ?: { uri ?: string ; range : any } } | null
1129 ) => void
1230) {
13- let filePath = fileURLToPath ( msg . params . textDocument . uri ) ;
14- let projectRootPath = utils . findProjectRootOfFile ( filePath ) ;
15- if ( projectRootPath == null ) {
31+ let executable = findExecutable ( msg . params . textDocument . uri ) ;
32+ if ( executable == null ) {
1633 onResult ( null ) ;
1734 } else {
18- // Currently assumes the dump command is "bin.exe" at the project root.
19- let commandPath = path . join ( projectRootPath , "bin.exe" ) ;
2035 let command =
21- commandPath +
36+ executable . binaryPath +
2237 " dump " +
23- filePath +
38+ executable . filePath +
2439 ":" +
2540 msg . params . position . line +
2641 ":" +
2742 msg . params . position . character ;
28- exec ( command , { cwd : projectRootPath } , function ( _error , _stdout , stderr ) {
43+ exec ( command , { cwd : executable . cwd } , function ( _error , _stdout , stderr ) {
2944 let result = JSON . parse ( stderr ) ;
3045 if ( result && result [ 0 ] ) {
3146 onResult ( result [ 0 ] ) ;
@@ -35,3 +50,39 @@ export function runDumpCommand(
3550 } ) ;
3651 }
3752}
53+
54+ export function runCompletionCommand (
55+ msg : RequestMessage ,
56+ code : string ,
57+ onResult : ( result : [ { label : string } ] | null ) => void
58+ ) {
59+ let executable = findExecutable ( msg . params . textDocument . uri ) ;
60+ if ( executable == null ) {
61+ onResult ( null ) ;
62+ } else {
63+ let tmpobj = tmp . fileSync ( ) ;
64+ let tmpname = tmpobj . name ;
65+ fs . writeFileSync ( tmpname , code , { encoding : "utf-8" } ) ;
66+
67+ let command =
68+ executable . binaryPath +
69+ " complete " +
70+ executable . filePath +
71+ ":" +
72+ msg . params . position . line +
73+ ":" +
74+ msg . params . position . character +
75+ " " +
76+ tmpname ;
77+
78+ exec ( command , { cwd : executable . cwd } , function ( _error , _stdout , stderr ) {
79+ tmpobj . removeCallback ( ) ;
80+ let result = JSON . parse ( stderr ) ;
81+ if ( result && result [ 0 ] ) {
82+ onResult ( result ) ;
83+ } else {
84+ onResult ( null ) ;
85+ }
86+ } ) ;
87+ }
88+ }
0 commit comments