Skip to content

Commit 80ced94

Browse files
committed
fix(internal/lsp): 修正 textDocument/hover 在空值是返回 {} 而不是 null 的错误
1 parent 114b2fb commit 80ced94

File tree

6 files changed

+48
-12
lines changed

6 files changed

+48
-12
lines changed

internal/lsp/protocol/enums.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ const (
119119
// MarkupKindPlainText plain text is supported as a content format
120120
MarkupKindPlainText MarkupKind = "plaintext"
121121

122-
// MarkupKinMarkdown markdown is supported as a content format
123-
MarkupKinMarkdown MarkupKind = "markdown"
122+
// MarkupKindMarkdown markdown is supported as a content format
123+
MarkupKindMarkdown MarkupKind = "markdown"
124124
)
125125

126126
// TextDocumentSyncKind defines how the host (editor) should sync document changes to the language server.

internal/lsp/protocol/hover.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
package protocol
44

5-
import "github.com/caixw/apidoc/v7/core"
5+
import (
6+
"encoding/json"
7+
8+
"github.com/caixw/apidoc/v7/core"
9+
)
610

711
// HoverCapabilities 客户端有关 hover 功能的描述
812
type HoverCapabilities struct {
@@ -24,9 +28,21 @@ type HoverParams struct {
2428
type Hover struct {
2529
// The hover's content
2630
// contents MarkedString | MarkedString[] | MarkupContent;
27-
Contents interface{} `json:"contents"`
31+
Contents MarkupContent `json:"contents"`
2832

2933
// An optional range is a range inside a text document
3034
// that is used to visualize a hover, e.g. by changing the background color.
3135
Range core.Range `json:"range,omitempty"`
3236
}
37+
38+
type hoverShadow Hover
39+
40+
// MarshalJSON 允许在 hover 为空值是返回 null
41+
func (h *Hover) MarshalJSON() ([]byte, error) {
42+
if h.Contents.Kind == "" {
43+
return json.Marshal(nil)
44+
}
45+
46+
shadow := (*hoverShadow)(h)
47+
return json.Marshal(shadow)
48+
}

internal/lsp/search/hover.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ var usagerType = reflect.TypeOf((*usager)(nil)).Elem()
2222
// 返回值表示是否找到了相应在的元素。
2323
func Hover(doc *ast.APIDoc, uri core.URI, pos core.Position, h *protocol.Hover) {
2424
u := doc.Search(uri, pos, usagerType)
25-
if u != nil {
26-
usage := u.(usager)
25+
if u == nil {
26+
return
27+
}
28+
29+
usage := u.(usager)
30+
if v := usage.Usage(); v != "" {
2731
h.Range = usage.R()
2832
h.Contents = protocol.MarkupContent{
29-
Kind: protocol.MarkupKinMarkdown,
30-
Value: usage.Usage(),
33+
Kind: protocol.MarkupKindMarkdown,
34+
Value: v,
3135
}
3236
}
3337
}

internal/lsp/search/hover_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestHover(t *testing.T) {
4545
Start: core.Position{Line: 1, Character: 1},
4646
End: core.Position{Line: 1, Character: 18},
4747
})
48-
a.Equal(hover.Contents.(protocol.MarkupContent).Value, locale.Sprintf("usage-apidoc-title"))
48+
a.Equal(hover.Contents.Value, locale.Sprintf("usage-apidoc-title"))
4949

5050
// apis[0]
5151
hover = &protocol.Hover{}
@@ -55,7 +55,7 @@ func TestHover(t *testing.T) {
5555
Start: core.Position{Line: 4, Character: 1},
5656
End: core.Position{Line: 7, Character: 7},
5757
})
58-
a.Equal(hover.Contents.(protocol.MarkupContent).Value, locale.Sprintf("usage-apidoc-apis"))
58+
a.Equal(hover.Contents.Value, locale.Sprintf("usage-apidoc-apis"))
5959

6060
// 改变了 api[0].URI
6161
doc.APIs[0].URI = core.URI("api0.go")
@@ -68,7 +68,7 @@ func TestHover(t *testing.T) {
6868
Start: core.Position{Line: 0, Character: 0},
6969
End: core.Position{Line: 12, Character: 9},
7070
})
71-
a.Equal(hover.Contents.(protocol.MarkupContent).Value, locale.Sprintf("usage-apidoc"))
71+
a.Equal(hover.Contents.Value, locale.Sprintf("usage-apidoc"))
7272

7373
// 与 apis[0] 相同的 URI
7474
hover = &protocol.Hover{}
@@ -78,5 +78,5 @@ func TestHover(t *testing.T) {
7878
Start: core.Position{Line: 4, Character: 1},
7979
End: core.Position{Line: 7, Character: 7},
8080
})
81-
a.Equal(hover.Contents.(protocol.MarkupContent).Value, locale.Sprintf("usage-apidoc-apis"))
81+
a.Equal(hover.Contents.Value, locale.Sprintf("usage-apidoc-apis"))
8282
}

internal/xmlenc/xmlenc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ type BaseTag struct {
3131

3232
// Usage 本地化的当前字段介绍内容
3333
func (b Base) Usage() string {
34+
if b.UsageKey == nil {
35+
return ""
36+
}
37+
3438
return locale.Sprintf(b.UsageKey)
3539
}
3640

internal/xmlenc/xmlenc_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"testing"
77

88
"github.com/issue9/assert"
9+
10+
"github.com/caixw/apidoc/v7/internal/locale"
911
)
1012

1113
func TestBaseTag_SelfClose(t *testing.T) {
@@ -26,3 +28,13 @@ func TestBaseTag_SelfClose(t *testing.T) {
2628
}
2729
a.False(b.SelfClose())
2830
}
31+
32+
func TestBase_Usage(t *testing.T) {
33+
a := assert.New(t)
34+
35+
b := Base{}
36+
a.Empty(b.Usage())
37+
38+
b.UsageKey = locale.ErrInvalidUTF8Character
39+
a.Equal(b.Usage(), locale.Sprintf(locale.ErrInvalidUTF8Character))
40+
}

0 commit comments

Comments
 (0)