-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grainlsp): Implement inlay hints for module includes (#1793)
* Add initial capability for inlay hints * Added inlay hint capability * Added simple test of printing out an inline value * Debug * Cleaned up and formatted the code * Nit cleanup after review * Remove whitespace --------- Co-authored-by: marcusroberts <[email protected]>
- Loading branch information
1 parent
4969d73
commit 408da76
Showing
8 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
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
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
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,84 @@ | ||
open Grain; | ||
open Compile; | ||
open Grain_parsing; | ||
open Grain_utils; | ||
open Grain_typed; | ||
open Grain_diagnostics; | ||
open Sourcetree; | ||
open Lsp_types; | ||
|
||
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#inlayHintParams | ||
module RequestParams = { | ||
[@deriving yojson({strict: false})] | ||
type t = { | ||
[@key "textDocument"] | ||
text_document: Protocol.text_document_identifier, | ||
range: Protocol.range, | ||
}; | ||
}; | ||
|
||
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint | ||
module ResponseResult = { | ||
[@deriving yojson] | ||
type inlay_hint = { | ||
label: string, | ||
position: Protocol.position, | ||
}; | ||
|
||
[@deriving yojson] | ||
type t = list(inlay_hint); | ||
}; | ||
|
||
let send_no_result = (~id: Protocol.message_id) => { | ||
Protocol.response(~id, `Null); | ||
}; | ||
|
||
let find_hints = program => { | ||
let hints = ref([]); | ||
open Typedtree; | ||
open Protocol; | ||
module Iterator = | ||
TypedtreeIter.MakeIterator({ | ||
include TypedtreeIter.DefaultIteratorArgument; | ||
let enter_toplevel_stmt = (stmt: toplevel_stmt) => { | ||
switch (stmt.ttop_desc) { | ||
| TTopInclude(inc) => | ||
let path = inc.tinc_path; | ||
let name = Path.name(inc.tinc_path); | ||
|
||
let stmt_loc = stmt.ttop_loc; | ||
let stmt_end = stmt_loc.loc_end; | ||
|
||
let p: Protocol.position = { | ||
line: stmt_end.pos_lnum - 1, | ||
character: stmt_end.pos_cnum - stmt_end.pos_bol + 1 + 1, | ||
}; | ||
|
||
let r: ResponseResult.inlay_hint = { | ||
label: ": " ++ name, | ||
position: p, | ||
}; | ||
hints := [r, ...hints^]; | ||
| _ => () | ||
}; | ||
}; | ||
}); | ||
Iterator.iter_typed_program(program); | ||
hints^; | ||
}; | ||
|
||
let process = | ||
( | ||
~id: Protocol.message_id, | ||
~compiled_code: Hashtbl.t(Protocol.uri, code), | ||
~documents: Hashtbl.t(Protocol.uri, string), | ||
params: RequestParams.t, | ||
) => { | ||
Trace.log("Inlay hint request received"); | ||
switch (Hashtbl.find_opt(compiled_code, params.text_document.uri)) { | ||
| None => send_no_result(~id) | ||
| Some({program, sourcetree}) => | ||
let hints = find_hints(program); | ||
Protocol.response(~id, ResponseResult.to_yojson(hints)); | ||
}; | ||
}; |
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,22 @@ | ||
open Grain_typed; | ||
|
||
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#inlayHintParams | ||
module RequestParams: { | ||
[@deriving yojson({strict: false})] | ||
type t; | ||
}; | ||
|
||
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint | ||
module ResponseResult: { | ||
[@deriving yojson] | ||
type t; | ||
}; | ||
|
||
let process: | ||
( | ||
~id: Protocol.message_id, | ||
~compiled_code: Hashtbl.t(Protocol.uri, Lsp_types.code), | ||
~documents: Hashtbl.t(Protocol.uri, string), | ||
RequestParams.t | ||
) => | ||
unit; |
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
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
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
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