Skip to content

Commit 5575343

Browse files
aviateskmlechu
andauthored
setup basic infra for completions (aviatesk#21)
For better collaborations on aviatesk#19 and aviatesk#20. Co-authored-by: Em Chu <[email protected]>
1 parent 659f78c commit 5575343

File tree

5 files changed

+592
-24
lines changed

5 files changed

+592
-24
lines changed

src/JETLS.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ using .LSP
1212
include("JSONRPC.jl")
1313
using .JSONRPC
1414

15-
include("utils.jl")
16-
1715
using Pkg, JuliaSyntax, JET
1816

1917
struct FileInfo
@@ -74,6 +72,9 @@ function ServerState(send::F) where F
7472
Ref{String}())
7573
end
7674

75+
include("utils.jl")
76+
include("completions.jl")
77+
7778
struct IncludeCallback <: Function
7879
file_cache::Dict{URI,FileInfo}
7980
end
@@ -146,6 +147,14 @@ function handle_message(state::ServerState, msg)
146147
return handle_DidSaveTextDocumentNotification(state, msg)
147148
elseif msg isa DocumentDiagnosticRequest || msg isa WorkspaceDiagnosticRequest
148149
@assert false
150+
elseif msg isa CompletionRequest
151+
try
152+
return handle_CompletionRequest(state, msg)
153+
catch e
154+
@info "Completion request failed:"
155+
Base.display_error(stderr, e, catch_backtrace())
156+
end
157+
return nothing
149158
else
150159
@warn "Unhandled message" msg
151160
return nothing
@@ -200,6 +209,8 @@ function initialize_result()
200209
change = TextDocumentSyncKind.Full,
201210
save = SaveOptions(;
202211
includeText = true)),
212+
completionProvider = CompletionOptions(resolveProvider=false,
213+
completionItem=(; labelDetailsSupport=true)),
203214
),
204215
serverInfo = (;
205216
name = "JETLS",

src/LSP/basic-json-structures.jl

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,23 +318,6 @@ Structure to capture a description for an error code.
318318
href::URI
319319
end
320320

321-
"""
322-
Represents a reference to a command. Provides a title which will be used to
323-
represent a command in the UI. Commands are identified by a string
324-
identifier. The recommended way to handle commands is to implement their
325-
execution on the server side if the client and server provides the corresponding
326-
capabilities. Alternatively the tool extension code could handle the
327-
command. The protocol currently doesn’t specify a set of well-known commands.
328-
"""
329-
@interface Command begin
330-
"Title of the command, like `save`."
331-
title::String
332-
"The identifier of the actual command handler."
333-
command::String
334-
"Arguments that the command handler should be invoked with"
335-
arguments::Union{Vector{Any}, Nothing} = nothing
336-
end
337-
338321
"""
339322
Represents a diagnostic, such as a compiler error or warning.
340323
Diagnostic objects are only valid in the scope of a resource.
@@ -395,6 +378,71 @@ Diagnostic objects are only valid in the scope of a resource.
395378
data::Union{Any, Nothing} = nothing
396379
end
397380

381+
"""
382+
Describes the content type that a client supports in various
383+
result literals like `Hover`, `ParameterInfo` or `CompletionItem`.
384+
Please note that `MarkupKinds` must not start with a `\$`. This kinds
385+
are reserved for internal usage.
386+
"""
387+
@namespace MarkupKind::String begin
388+
"Plain text is supported as a content format"
389+
PlainText = "plaintext"
390+
"Markdown is supported as a content format"
391+
Markdown = "markdown"
392+
end
393+
394+
"""
395+
A `MarkupContent` literal represents a string value which content is
396+
interpreted base on its kind flag. Currently the protocol supports
397+
`plaintext` and `markdown` as markup kinds.
398+
399+
If the kind is `markdown` then the value can contain fenced code blocks like
400+
in GitHub issues.
401+
402+
Here is an example how such a string can be constructed using
403+
JavaScript / TypeScript:
404+
```typescript
405+
let markdown: MarkdownContent = {
406+
kind: MarkupKind.Markdown,
407+
value: [
408+
'# Header',
409+
'Some text',
410+
'```typescript',
411+
'someCode();',
412+
'```',
413+
].join('\n')
414+
};
415+
```
416+
417+
*Please Note* that clients might sanitize the return markdown. A client could
418+
decide to remove HTML from the markdown to avoid script execution.
419+
*/
420+
"""
421+
@interface MarkupContent begin
422+
"The type of the Markup"
423+
kind::MarkupKind.Ty
424+
425+
"The content itself"
426+
value::String
427+
end
428+
429+
"""
430+
Represents a reference to a command. Provides a title which will be used to
431+
represent a command in the UI. Commands are identified by a string
432+
identifier. The recommended way to handle commands is to implement their
433+
execution on the server side if the client and server provides the corresponding
434+
capabilities. Alternatively the tool extension code could handle the
435+
command. The protocol currently doesn’t specify a set of well-known commands.
436+
"""
437+
@interface Command begin
438+
"Title of the command, like `save`."
439+
title::String
440+
"The identifier of the actual command handler."
441+
command::String
442+
"Arguments that the command handler should be invoked with"
443+
arguments::Union{Vector{Any}, Nothing} = nothing
444+
end
445+
398446
# Work done progress
399447
# ==================
400448

src/LSP/language-features/completions.jl

Lines changed: 162 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ request is triggered.
8686
triggerCharacter::Union{String, Nothing} = nothing
8787
end
8888

89-
"""
90-
Request method: textDocument/completion
91-
"""
9289
@interface CompletionParams @extends TextDocumentPositionParams, WorkDoneProgressParams, PartialResultParams begin
9390
"""
9491
The completion context. This is only available if the client specifies
@@ -98,6 +95,11 @@ Request method: textDocument/completion
9895
context::Union{CompletionContext, Nothing} = nothing
9996
end
10097

98+
@interface CompletionRequest @extends RequestMessage begin
99+
method::String = "textDocument/completion"
100+
params::CompletionParams
101+
end
102+
101103
"""
102104
Defines whether the insert text in a completion item should be interpreted as
103105
plain text or a snippet.
@@ -274,8 +276,7 @@ end
274276
detail::Union{String, Nothing} = nothing
275277

276278
"A human-readable string that represents a doc-comment."
277-
# documentation::Union{Union{MarkupContent, String}, Nothing} = nothing
278-
documentation::Union{String, Nothing} = nothing
279+
documentation::Union{MarkupContent, String, Nothing} = nothing
279280

280281
"""
281282
Indicates if this item is deprecated.
@@ -499,3 +500,159 @@ presented in the editor.
499500
"""
500501
items::Vector{CompletionItem}
501502
end
503+
504+
@interface CompletionClientCapabilities begin
505+
"""
506+
Whether completion supports dynamic registration.
507+
"""
508+
dynamicRegistration::Union{Nothing, Bool} = nothing
509+
510+
"""
511+
The client supports the following `CompletionItem` specific
512+
capabilities.
513+
"""
514+
completionItem::Union{Nothing, @interface begin
515+
"""
516+
Client supports snippets as insert text.
517+
518+
A snippet can define tab stops and placeholders with `\$1`, `\$2`
519+
and `\${3:foo}`. `\$0` defines the final tab stop, it defaults to
520+
the end of the snippet. Placeholders with equal identifiers are
521+
linked, that is typing in one will update others too.
522+
"""
523+
snippetSupport::Union{Nothing, Bool} = nothing
524+
525+
"""
526+
Client supports commit characters on a completion item.
527+
"""
528+
commitCharactersSupport::Union{Nothing, Bool} = nothing
529+
530+
"""
531+
Client supports the follow content formats for the documentation
532+
property. The order describes the preferred format of the client.
533+
"""
534+
documentationFormat::Union{Nothing, Vector{MarkupKind.Ty}} = nothing
535+
536+
"""
537+
Client supports the deprecated property on a completion item.
538+
"""
539+
deprecatedSupport::Union{Nothing, Bool} = nothing
540+
541+
"""
542+
Client supports the preselect property on a completion item.
543+
"""
544+
preselectSupport::Union{Nothing, Bool} = nothing
545+
546+
"""
547+
Client supports the tag property on a completion item. Clients
548+
supporting tags have to handle unknown tags gracefully. Clients
549+
especially need to preserve unknown tags when sending a completion
550+
item back to the server in a resolve call.
551+
552+
# Tags
553+
- since - 3.15.0
554+
"""
555+
tagSupport::Union{Nothing, @interface begin
556+
"""
557+
The tags supported by the client.
558+
"""
559+
valueSet::Vector{CompletionItemTag.Ty}
560+
end} = nothing
561+
562+
"""
563+
Client supports insert replace edit to control different behavior if
564+
a completion item is inserted in the text or should replace text.
565+
566+
# Tags
567+
- since - 3.16.0
568+
"""
569+
insertReplaceSupport::Union{Nothing, Bool} = nothing
570+
571+
"""
572+
Indicates which properties a client can resolve lazily on a
573+
completion item. Before version 3.16.0 only the predefined properties
574+
`documentation` and `detail` could be resolved lazily.
575+
576+
# Tags
577+
- since - 3.16.0
578+
"""
579+
resolveSupport::Union{Nothing, @interface begin
580+
"""
581+
The properties that a client can resolve lazily.
582+
"""
583+
properties::Vector{String}
584+
end} = nothing
585+
586+
"""
587+
The client supports the `insertTextMode` property on
588+
a completion item to override the whitespace handling mode
589+
as defined by the client (see `insertTextMode`).
590+
591+
# Tags
592+
- since - 3.16.0
593+
"""
594+
insertTextModeSupport::Union{Nothing, @interface begin
595+
valueSet::Vector{InsertTextMode.Ty}
596+
end} = nothing
597+
598+
"""
599+
The client has support for completion item label
600+
details (see also `CompletionItemLabelDetails`).
601+
602+
# Tags
603+
- since - 3.17.0
604+
"""
605+
labelDetailsSupport::Union{Nothing, Bool} = nothing
606+
end} = nothing
607+
608+
completionItemKind::Union{Nothing, @interface begin
609+
"""
610+
The completion item kind values the client supports. When this
611+
property exists the client also guarantees that it will
612+
handle values outside its set gracefully and falls back
613+
to a default value when unknown.
614+
615+
If this property is not present the client only supports
616+
the completion items kinds from `Text` to `Reference` as defined in
617+
the initial version of the protocol.
618+
"""
619+
valueSet::Union{Nothing, Vector{CompletionItemKind.Ty}} = nothing
620+
end} = nothing
621+
622+
"""
623+
The client supports to send additional context information for a
624+
`textDocument/completion` request.
625+
"""
626+
contextSupport::Union{Nothing, Bool} = nothing
627+
628+
"""
629+
The client's default when the completion item doesn't provide a
630+
`insertTextMode` property.
631+
632+
# Tags
633+
- since - 3.17.0
634+
"""
635+
insertTextMode::Union{Nothing, InsertTextMode.Ty} = nothing
636+
637+
"""
638+
The client supports the following `CompletionList` specific
639+
capabilities.
640+
641+
# Tags
642+
- since - 3.17.0
643+
"""
644+
completionList::Union{Nothing, @interface begin
645+
"""
646+
The client supports the following itemDefaults on
647+
a completion list.
648+
649+
The value lists the supported property names of the
650+
`CompletionList.itemDefaults` object. If omitted
651+
no properties are supported.
652+
653+
# Tags
654+
- since - 3.17.0
655+
"""
656+
itemDefaults::Union{Nothing, Vector{String}} = nothing
657+
end} = nothing
658+
end

0 commit comments

Comments
 (0)