Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inlay hints support #53

Merged
merged 9 commits into from
Nov 6, 2023
72 changes: 65 additions & 7 deletions nimlangserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ proc initialize(ls: LanguageServer, params: InitializeParams):
save: some(SaveOptions(includeText: some(true))))
),
hoverProvider: some(true),
workspace: WorkspaceCapability(
workspaceFolders: some(WorkspaceFolderCapability())
),
workspace: some(ServerCapabilities_workspace(
workspaceFolders: some(WorkspaceFoldersServerCapabilities())
)),
completionProvider: CompletionOptions(
triggerCharacters: some(@["."]),
resolveProvider: some(false)
Expand All @@ -183,9 +183,12 @@ proc initialize(ls: LanguageServer, params: InitializeParams):
referencesProvider: some(true),
documentHighlightProvider: some(true),
workspaceSymbolProvider: some(true),
executeCommandProvider: ExecuteCommandOptions(
executeCommandProvider: some(ExecuteCommandOptions(
commands: some(@[RESTART_COMMAND, RECOMPILE_COMMAND, CHECK_PROJECT_COMMAND])
),
)),
inlayHintProvider: some(InlayHintOptions(
resolveProvider: some(false)
)),
documentSymbolProvider: some(true),
codeActionProvider: some(true)
)
Expand Down Expand Up @@ -298,7 +301,7 @@ proc progressSupported(ls: LanguageServer): bool =
result = ls.initializeParams
.capabilities
.window
.get(WindowCapabilities())
.get(ClientCapabilities_window())
.workDoneProgress
.get(false)

Expand Down Expand Up @@ -677,7 +680,7 @@ proc expand(ls: LanguageServer, params: ExpandTextDocumentPositionParams):
.await()
if expand.len != 0:
result = ExpandResult(content: expand[0].doc.fixIdentation(character),
range: createRangeFromSuggest(expand[0]))
range: expand[0].createRangeFromSuggest())

proc typeDefinition(ls: LanguageServer, params: TextDocumentPositionParams, id: int):
Future[seq[Location]] {.async.} =
Expand Down Expand Up @@ -745,6 +748,60 @@ proc rename(ls: LanguageServer, params: RenameParams, id: int): Future[Workspace
edits[reference.uri] &= %TextEdit(range: reference.range, newText: params.newName)
result = WorkspaceEdit(changes: some edits)

proc convertInlayHintKind(kind: SuggestInlayHintKind): InlayHintKind_int =
case kind
of sihkType:
result = 1
of sihkParameter:
result = 2

proc toInlayHint(suggest: SuggestInlayHint): InlayHint =
let hint_line = suggest.line - 1
# TODO: how to convert column?
var hint_col = suggest.column
result = InlayHint(
position: Position(
line: hint_line,
character: hint_col
),
label: suggest.label,
kind: some(convertInlayHintKind(suggest.kind)),
paddingLeft: some(suggest.paddingLeft),
paddingRight: some(suggest.paddingRight)
)
if suggest.allowInsert:
result.textEdits = some(@[
TextEdit(
newText: suggest.label,
`range`: Range(
start: Position(
line: hint_line,
character: hint_col
),
`end`: Position(
line: hint_line,
character: hint_col
)
)
)
])

proc inlayHint(ls: LanguageServer, params: InlayHintParams, id: int): Future[seq[InlayHint]] {.async.} =
debug "inlayHint received..."
with (params.range, params.textDocument):
let
nimsuggest = await ls.getNimsuggest(uri)
suggestions = await nimsuggest
.inlayHints(uriToPath(uri),
ls.uriToStash(uri),
start.line + 1,
ls.getCharacter(uri, start.line, start.character),
`end`.line + 1,
ls.getCharacter(uri, `end`.line, `end`.character))
.orCancelled(ls, id)
result = suggestions
.map(x => x.inlayHintInfo.toInlayHint());

proc codeAction(ls: LanguageServer, params: CodeActionParams):
Future[seq[CodeAction]] {.async.} =
let projectUri = await getProjectFile(params.textDocument.uri.uriToPath, ls)
Expand Down Expand Up @@ -914,6 +971,7 @@ proc registerHandlers*(connection: StreamConnection,
connection.register("textDocument/codeAction", partial(codeAction, ls))
connection.register("textDocument/prepareRename", partial(prepareRename, ls))
connection.register("textDocument/rename", partial(rename, ls))
connection.register("textDocument/inlayHint", partial(inlayHint, ls))
connection.register("workspace/executeCommand", partial(executeCommand, ls))
connection.register("workspace/symbol", partial(workspaceSymbol, ls))
connection.register("textDocument/documentHighlight", partial(documentHighlight, ls))
Expand Down
2 changes: 2 additions & 0 deletions nimlangserver.nim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
--define:"chronicles_colors=None"
--define:"chronicles_disable_thread_id"
--define:"debugLogging"
#--define:"chronicles_log_level=TRACE"
--define:"chronicles_log_level=DEBUG"
--define:"chronicles_timestamps=None"
#--define:"chronicles_sinks=textlines[file]"
34 changes: 31 additions & 3 deletions protocol/enums.nim
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
type
ErrorCode* = enum
RequestCancelled = -32800 # All the other error codes are from JSON-RPC
RequestFailed = -32803,
ServerCancelled = -32802,
ContentModified = -32801,
RequestCancelled = -32800, # All the other error codes are from JSON-RPC
ParseError = -32700,
InternalError = -32603,
InvalidParams = -32602,
MethodNotFound = -32601,
InvalidRequest = -32600,
ServerErrorStart = -32099,
ServerNotInitialized = -32002,
ServerErrorEnd = -32000,
UnknownErrorCode = -32001,
ServerErrorEnd = -32000
# Anything below here comes from the LSP specification
type
DiagnosticSeverity* {.pure.} = enum
Error = 1,
Warning = 2,
Information = 3,
Hint = 4
DiagnosticTag* {.pure.} = enum
Unnecessary = 1,
Deprecated = 2
SymbolKind* {.pure.} = enum
File = 1,
Module = 2,
Expand Down Expand Up @@ -77,7 +84,8 @@ type
Error = 1,
Warning = 2,
Info = 3,
Log = 4
Log = 4,
Debug = 5
FileChangeType* {.pure.} = enum
Created = 1,
Changed = 2,
Expand Down Expand Up @@ -105,3 +113,23 @@ type
Invoked = 1,
TriggerCharacter = 2,
ContentChange = 3
InitializeErrorCodes* {.pure.} = enum
unknownProtocolVersion = 1
NotebookCellKind* {.pure.} = enum
Markup = 1,
Code = 2
SymbolTag* {.pure.} = enum
Deprecated = 1
InlayHintKind* {.pure.} = enum
Type = 1,
Parameter = 2
CompletionItemTag* {.pure.} = enum
Deprecated = 1
InsertTextMode* {.pure.} = enum
asIs = 1,
adjustIndentation = 2
CodeActionTriggerKind* {.pure.} = enum
Invoked = 1,
Automatic = 2
PrepareSupportDefaultBehavior* {.pure.} = enum
Identifier = 1
Loading