Skip to content

Commit f07dcb5

Browse files
committed
Merge branch 'dev.lsp'
2 parents 6b7ebbb + d4ccfb4 commit f07dcb5

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed

internal/lsp/folder.go

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func (f *folder) messageHandler(msg *core.Message) {
8383
default:
8484
panic("unreached")
8585
}
86+
87+
f.srv.textDocumentPublishDiagnostics(f.URI, f.errors, f.warns)
8688
}
8789

8890
func (s *server) appendFolders(folders ...protocol.WorkspaceFolder) (err error) {

internal/lsp/protocol/diagnostic.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
package protocol
4+
5+
import "github.com/caixw/apidoc/v7/core"
6+
7+
// PublishDiagnosticsParams textDocument/publishDiagnostics 事件发送的参数
8+
type PublishDiagnosticsParams struct {
9+
// The URI for which diagnostic information is reported.
10+
URI core.URI `json:"uri"`
11+
12+
// Optional the version number of the document the diagnostics are published for.
13+
//
14+
// @since 3.15.0
15+
Version int `json:"version,omitempty"`
16+
17+
// An array of diagnostic information items.
18+
Diagnostics []Diagnostic `json:"diagnostics"`
19+
}
20+
21+
// Diagnostic represents a diagnostic,such as a compiler error or warning.
22+
// Diagnostic objects are only valid in the scope of a resource.
23+
//
24+
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#diagnostic
25+
type Diagnostic struct {
26+
// The range at which the message applies.
27+
Range core.Range `json:"range"`
28+
29+
// The diagnostic's severity. Can be omitted. If omitted it is up to the
30+
// client to interpret diagnostics as error, warning, info or hint.
31+
Severity DiagnosticSeverity `json:"severity,omitempty"`
32+
33+
// The diagnostic's code, which might appear in the user interface.
34+
Code string `json:"code,omitempty"`
35+
36+
// A human-readable string describing the source of this
37+
// diagnostic, e.g. 'typescript' or 'super lint'.
38+
Source string `json:"source,omitempty"`
39+
40+
// The diagnostic's message.
41+
Message string `json:"message"`
42+
43+
// Additional metadata about the diagnostic.
44+
//
45+
// @since 3.15.0
46+
Tags []DiagnosticTag `json:"tags,omitempty"`
47+
48+
// An array of related diagnostic information, e.g. when symbol-names within
49+
// a scope collide all definitions can be marked via this property.
50+
RelatedInformation []DiagnosticRelatedInformation `json:"relatedInformation,omitempty"`
51+
}
52+
53+
type DiagnosticSeverity int
54+
55+
// DiagnosticSeverity 可用的常量
56+
const (
57+
DiagnosticSeverityError DiagnosticSeverity = iota + 1 // Reports an error
58+
DiagnosticSeverityWarning // Reports a warning
59+
DiagnosticSeverityInformation // Reports an information
60+
DiagnosticSeverityHint // Reports a hint
61+
)
62+
63+
// DiagnosticTag the diagnostic tags.
64+
//
65+
// @since 3.15.0
66+
type DiagnosticTag int
67+
68+
// DiagnosticTag 可用的常量列表
69+
const (
70+
// DiagnosticTagUnnecessary unused or unnecessary code.
71+
//
72+
// Clients are allowed to render diagnostics with this tag faded out instead of having
73+
// an error squiggle.
74+
DiagnosticTagUnnecessary DiagnosticTag = 1
75+
76+
// DiagnosticTagDeprecated deprecated or obsolete code.
77+
//
78+
// Clients are allowed to rendered diagnostics with this tag strike through.
79+
DiagnosticTagDeprecated DiagnosticTag = 2
80+
)
81+
82+
// DiagnosticRelatedInformation represents a related message and source code location for a diagnostic
83+
//
84+
// This should be used to point to code locations that cause or are related to a diagnostics,
85+
// e.g when duplicating a symbol in a scope.
86+
type DiagnosticRelatedInformation struct {
87+
// The location of this related diagnostic information.
88+
Location core.Location `json:"location"`
89+
90+
// The message of this related diagnostic information.
91+
Message string `json:"message"`
92+
}

internal/lsp/protocol/textdocument.go

+16
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,23 @@ type TextDocumentClientCapabilities struct {
293293
PublishDiagnostics struct {
294294
// Whether the clients accepts diagnostics with related information.
295295
RelatedInformation bool `json:"relatedInformation,omitempty"`
296+
297+
// Client supports the tag property to provide meta data about a diagnostic.
298+
// Clients supporting tags have to handle unknown tags gracefully.
299+
//
300+
// @since 3.15.0
301+
TagSupport struct {
302+
// The tags supported by the client.
303+
ValueSet []DiagnosticTag `json:"valueSet,omitempty"`
304+
} `json:"tagSupport,omitempty"`
305+
306+
// Whether the client interprets the version property of the
307+
// `textDocument/publishDiagnostics` notification's parameter.
308+
//
309+
// @since 3.15.0
310+
VersionSupport bool `json:"versionSupport,omitempty"`
296311
} `json:"publishDiagnostics,omitempty"`
312+
297313
// Capabilities specific to `textDocument/foldingRange` requests.
298314
//
299315
// Since 3.10.0

internal/lsp/textdocument.go

+33-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package lsp
44

55
import (
6-
"fmt"
76
"reflect"
87
"strings"
98

@@ -73,7 +72,6 @@ func (f *folder) matchPosition(uri core.URI, pos core.Position) (bool, error) {
7372
//
7473
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_hover
7574
func (s *server) textDocumentHover(notify bool, in *protocol.HoverParams, out *protocol.Hover) error {
76-
fmt.Println("INFO")
7775
for _, f := range s.folders {
7876
f.searchHover(in.TextDocument.URI, in.TextDocumentPositionParams.Position, out)
7977

@@ -103,3 +101,36 @@ func (f *folder) searchHover(uri core.URI, pos core.Position, hover *protocol.Ho
103101
}
104102
}
105103
}
104+
105+
// textDocument/publishDiagnostics
106+
//
107+
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_publishDiagnostics
108+
func (s *server) textDocumentPublishDiagnostics(uri core.URI, errs []*core.SyntaxError, warns []*core.SyntaxError) error {
109+
110+
if s.clientCapabilities.TextDocument.PublishDiagnostics.RelatedInformation == false {
111+
return nil
112+
}
113+
114+
p := &protocol.PublishDiagnosticsParams{
115+
URI: uri,
116+
Diagnostics: make([]protocol.Diagnostic, 0, len(errs)+len(warns)),
117+
}
118+
119+
for _, err := range errs {
120+
p.Diagnostics = append(p.Diagnostics, protocol.Diagnostic{
121+
Range: err.Location.Range,
122+
Message: err.Error(),
123+
Severity: protocol.DiagnosticSeverityError,
124+
})
125+
}
126+
127+
for _, warn := range warns {
128+
p.Diagnostics = append(p.Diagnostics, protocol.Diagnostic{
129+
Range: warn.Location.Range,
130+
Message: warn.Error(),
131+
Severity: protocol.DiagnosticSeverityWarning,
132+
})
133+
}
134+
135+
return s.Notify("textDocument/publishDiagnostics", p)
136+
}

0 commit comments

Comments
 (0)