-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from rescript-lang/runDumpCommand
Hook up command from rescript-editor-support.
- Loading branch information
Showing
5 changed files
with
149 additions
and
15 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { fileURLToPath } from "url"; | ||
import { RequestMessage } from "vscode-languageserver"; | ||
import * as utils from "./utils"; | ||
import * as path from "path"; | ||
import { exec } from "child_process"; | ||
import * as tmp from "tmp"; | ||
import fs from "fs"; | ||
|
||
let binaryPath = path.join( | ||
path.dirname(__dirname), | ||
process.platform, | ||
"rescript-editor-support.exe" | ||
); | ||
|
||
export let binaryExists = fs.existsSync(binaryPath); | ||
|
||
let findExecutable = (uri: string) => { | ||
let filePath = fileURLToPath(uri); | ||
let projectRootPath = utils.findProjectRootOfFile(filePath); | ||
if (projectRootPath == null || !binaryExists) { | ||
return null; | ||
} else { | ||
return { binaryPath, filePath, cwd: projectRootPath }; | ||
} | ||
}; | ||
|
||
export function runDumpCommand( | ||
msg: RequestMessage, | ||
onResult: ( | ||
result: { hover?: string; definition?: { uri?: string; range: any } } | null | ||
) => void | ||
) { | ||
let executable = findExecutable(msg.params.textDocument.uri); | ||
if (executable == null) { | ||
onResult(null); | ||
} else { | ||
let command = | ||
executable.binaryPath + | ||
" dump " + | ||
executable.filePath + | ||
":" + | ||
msg.params.position.line + | ||
":" + | ||
msg.params.position.character; | ||
exec(command, { cwd: executable.cwd }, function (_error, stdout, _stderr) { | ||
let result = JSON.parse(stdout); | ||
if (result && result[0]) { | ||
onResult(result[0]); | ||
} else { | ||
onResult(null); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export function runCompletionCommand( | ||
msg: RequestMessage, | ||
code: string, | ||
onResult: (result: [{ label: string }] | null) => void | ||
) { | ||
let executable = findExecutable(msg.params.textDocument.uri); | ||
if (executable == null) { | ||
onResult(null); | ||
} else { | ||
let tmpobj = tmp.fileSync(); | ||
let tmpname = tmpobj.name; | ||
fs.writeFileSync(tmpname, code, { encoding: "utf-8" }); | ||
|
||
let command = | ||
executable.binaryPath + | ||
" complete " + | ||
executable.filePath + | ||
":" + | ||
msg.params.position.line + | ||
":" + | ||
msg.params.position.character + | ||
" " + | ||
tmpname; | ||
|
||
exec(command, { cwd: executable.cwd }, function (_error, stdout, _stderr) { | ||
tmpobj.removeCallback(); | ||
let result = JSON.parse(stdout); | ||
if (result && result[0]) { | ||
onResult(result); | ||
} else { | ||
onResult(null); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.