Skip to content

Commit df91849

Browse files
marcusrobertsMarcus Robertsphatedospencer
authored
feat(lsp)!: Replaced one-off LSP command with persistent LSP server (#1131)
fix(compiler): Avoid module aliases of themselves chore(compiler): Add char_at utility chore(compiler): Remove an extra Option binding in filepath resolution chore(compiler): Introduce grain_formatting library chore(compiler): Introduce language_server library chore(compiler)!: Remove `--lsp` flag from grainc executable chore(compiler): Add `grainlsp` executable Co-authored-by: Marcus Roberts <marcus@marcusr.com> Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com> Co-authored-by: Oscar Spencer <oscar@grain-lang.org>
1 parent 97f99f4 commit df91849

File tree

65 files changed

+2747
-447
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2747
-447
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@ cli/bin/graindoc.exe
4141
cli/bin/graindoc.bc.js
4242
cli/bin/grainformat.exe
4343
cli/bin/grainformat.bc.js
44+
cli/bin/grainlsp.exe
45+
cli/bin/grainlsp.bc.js
4446

4547
.DS_Store

cli/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bin/grainc.bc.js
22
bin/graindoc.bc.js
33
bin/grainformat.bc.js
4+
bin/grainlsp.bc.js
45
CHANGELOG.md

cli/bin/exec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,30 @@ function execGrainformat(
9393
);
9494
}
9595

96+
function getGrainlsp() {
97+
const grainlsp = path.join(__dirname, "grainlsp.exe");
98+
99+
// TODO: Maybe make an installable path & check it?
100+
if (process.pkg || !fs.existsSync(grainlsp)) {
101+
const node = process.execPath;
102+
const grainlsp_js = path.join(__dirname, "grainlsp.js");
103+
return `"${node}" ${grainlsp_js}`;
104+
}
105+
106+
return `${grainlsp}`;
107+
}
108+
109+
const grainlsp = getGrainlsp();
110+
111+
function execGrainlsp(options, program, execOpts = { stdio: "inherit" }) {
112+
const flags = flagsFromOptions(program, options);
113+
114+
return execSync(`${grainlsp} ${flags.join(" ")}`, execOpts);
115+
}
116+
96117
module.exports = {
97118
grainc: execGrainc,
98119
graindoc: execGraindoc,
99120
grainformat: execGrainformat,
121+
grainlsp: execGrainlsp,
100122
};

cli/bin/grain.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,9 @@ program
218218
.action(run);
219219

220220
program
221-
.command("lsp <file>")
222-
.description("check a grain file for LSP")
223-
.action(function (file, options, program) {
224-
// The lsp subcommand inherits all options of the
225-
// top level grain command
226-
227-
// call the compiler, passing stdio through so the compiler gets the source code on stdin
228-
// and we get the compiler output in stdout
229-
// we still take the file name so we have it available
230-
231-
exec.grainc(`--lsp ${file}`, options, program);
232-
});
221+
.command("lsp")
222+
.description("start the Grain LSP server")
223+
.action(exec.grainlsp);
233224

234225
program
235226
.command("doc <file|dir>")

cli/bin/grainlsp.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// A wrapper around grainlsp.bc.js that prepares the `pkg` env
2+
require("./pkg");
3+
require("./grainlsp.bc.js");

compiler/dune

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(alias
22
(name native)
33
(deps grainc/grainc.exe graindoc/graindoc.exe grainformat/grainformat.exe
4-
test/test.exe))
4+
grainlsp/grainlsp.exe test/test.exe))
55

66
(alias
77
(name js)
88
(deps grainc/grainc.bc.js graindoc/graindoc.bc.js
9-
grainformat/grainformat.bc.js test/test.bc.js))
9+
grainformat/grainformat.bc.js grainlsp/grainlsp.bc.js test/test.bc.js))

compiler/dune-project

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@
3030
(name grainformat)
3131
(synopsis "Grain formatter"))
3232

33+
(package
34+
(name grainlsp)
35+
(synopsis "Grain LSP"))
36+
37+
(package
38+
(name grain_language_server)
39+
(synopsis "Grain Language Server Protocol"))
40+
41+
(package
42+
(name grain_formatting)
43+
(synopsis "Grain formatting"))
44+
3345
(package
3446
(name grain_linking)
3547
(synopsis "The Grain linker"))

compiler/esy.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
"name": "@grain/compiler",
33
"version": "0.4.6",
44
"esy": {
5-
"build": [
6-
"dune build @native --no-buffer"
7-
],
5+
"build": ["dune build @native --no-buffer"],
86
"buildEnv": {
97
"DUNE_BUILD_DIR": "#{self.target_dir}",
108
"GRAIN_STDLIB": "#{self.root / '..' / 'stdlib'}",
@@ -14,14 +12,16 @@
1412
"GRAINDOC_JS_PATH": "#{self.target_dir / 'default' / 'graindoc' / 'graindoc.bc.js'}",
1513
"GRAINFORMAT_BIN_PATH": "#{self.target_dir / 'default' / 'grainformat' / 'grainformat.exe'}",
1614
"GRAINFORMAT_JS_PATH": "#{self.target_dir / 'default' / 'grainformat' / 'grainformat.bc.js'}",
15+
"GRAINLSP_BIN_PATH": "#{self.target_dir / 'default' / 'grainlsp' / 'grainlsp.exe'}",
16+
"GRAINLSP_JS_PATH": "#{self.target_dir / 'default' / 'grainlsp' / 'grainlsp.bc.js'}",
1717
"CLI_BIN_DIR": "#{self.root / '..' / 'cli' / 'bin'}",
1818
"TEST_EXEC_PATH": "#{self.target_dir / 'default' / 'test' / 'test.exe'}",
1919
"TEST_JS_PATH": "#{self.target_dir / 'default' / 'test' / 'test.bc.js'}"
2020
}
2121
},
2222
"scripts": {
23-
"copy:exe": "cp #{$GRAINC_BIN_PATH} #{$GRAINDOC_BIN_PATH} #{$GRAINFORMAT_BIN_PATH} #{$CLI_BIN_DIR}",
24-
"copy:js": "cp #{$GRAINC_JS_PATH} #{$GRAINDOC_JS_PATH} #{$GRAINFORMAT_JS_PATH} #{$CLI_BIN_DIR}",
23+
"copy:exe": "cp #{$GRAINC_BIN_PATH} #{$GRAINDOC_BIN_PATH} #{$GRAINFORMAT_BIN_PATH} #{$GRAINLSP_BIN_PATH} #{$CLI_BIN_DIR}",
24+
"copy:js": "cp #{$GRAINC_JS_PATH} #{$GRAINDOC_JS_PATH} #{$GRAINFORMAT_JS_PATH} #{$GRAINLSP_JS_PATH} #{$CLI_BIN_DIR}",
2525
"clean": "rm -rf #{self.root}/_esy",
2626
"test": "#{$TEST_EXEC_PATH}",
2727
"test:js": "node #{$TEST_JS_PATH}",
@@ -38,11 +38,13 @@
3838
"@opam/fs": "0.0.2",
3939
"@opam/menhir": "20211125",
4040
"@opam/ocamlgraph": ">= 2.0.0",
41+
"@opam/ppx_deriving": ">= 5.2.1",
4142
"@opam/ppx_deriving_cmdliner": ">= 0.6.0",
4243
"@opam/ppx_deriving_yojson": ">= 3.5.2",
4344
"@opam/ppx_sexp_conv": ">= 0.14.0",
4445
"@opam/reason": ">= 3.6.2",
4546
"@opam/sexplib": ">= 0.14.0",
47+
"@opam/uri": ">= 4.2.0 < 5.0.0",
4648
"@opam/utf8": "0.1.0",
4749
"@opam/yojson": ">= 1.7.0",
4850
"ocaml": "4.12.0"

0 commit comments

Comments
 (0)