Skip to content

Commit ae7cbe0

Browse files
committed
Added textDocument/documentSymbol
1 parent 9ac2644 commit ae7cbe0

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

Diff for: handler/handler.go

+32
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ func (handler *InoHandler) transformClangdParams(method string, raw *json.RawMes
142142
p := params.(*lsp.DocumentOnTypeFormattingParams)
143143
uri = p.TextDocument.URI
144144
err = handler.ino2cppDocumentOnTypeFormattingParams(p)
145+
case "textDocument/documentSymbol":
146+
p := params.(*lsp.DocumentSymbolParams)
147+
uri = p.TextDocument.URI
148+
err = handler.ino2cppTextDocumentIdentifier(&p.TextDocument)
145149
}
146150
return
147151
}
@@ -324,6 +328,9 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
324328
for index := range *r {
325329
handler.cpp2inoTextEdit(&(*r)[index], uri)
326330
}
331+
case "textDocument/documentSymbol":
332+
r := result.(*[]DocumentSymbol)
333+
handler.cpp2inoDocumentSymbol(r, uri)
327334
}
328335
return result
329336
}
@@ -402,6 +409,31 @@ func (handler *InoHandler) cpp2inoTextEdit(edit *lsp.TextEdit, uri lsp.DocumentU
402409
}
403410
}
404411

412+
func (handler *InoHandler) cpp2inoDocumentSymbol(symbols *[]DocumentSymbol, uri lsp.DocumentURI) {
413+
if data, ok := handler.data[uri]; ok {
414+
for i := 0; i < len(*symbols); {
415+
symbol := &(*symbols)[i]
416+
symbol.Range.Start.Line = data.sourceLineMap[symbol.Range.Start.Line]
417+
symbol.Range.End.Line = data.sourceLineMap[symbol.Range.End.Line]
418+
symbol.SelectionRange.Start.Line = data.sourceLineMap[symbol.SelectionRange.Start.Line]
419+
symbol.SelectionRange.End.Line = data.sourceLineMap[symbol.SelectionRange.End.Line]
420+
421+
duplicate := false
422+
for j := 0; j < i; j++ {
423+
if symbol.Name == (*symbols)[j].Name && symbol.Range.Start.Line == (*symbols)[j].Range.Start.Line {
424+
duplicate = true
425+
break
426+
}
427+
}
428+
if duplicate {
429+
*symbols = (*symbols)[:i+copy((*symbols)[i:], (*symbols)[i+1:])]
430+
} else {
431+
i++
432+
}
433+
}
434+
}
435+
}
436+
405437
// FromClangd handles a message received from clangd.
406438
func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.Conn, req *jsonrpc2.Request) (interface{}, error) {
407439
params, _, err := handler.transformStdioParams(req.Method, req.Params)

Diff for: handler/protocol.go

+19
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func readParams(method string, raw *json.RawMessage) (interface{}, error) {
6262
params := new(lsp.DocumentOnTypeFormattingParams)
6363
err := json.Unmarshal(*raw, params)
6464
return params, err
65+
case "textDocument/documentSymbol":
66+
params := new(lsp.DocumentSymbolParams)
67+
err := json.Unmarshal(*raw, params)
68+
return params, err
6569
case "textDocument/publishDiagnostics":
6670
params := new(lsp.PublishDiagnosticsParams)
6771
err := json.Unmarshal(*raw, params)
@@ -114,6 +118,10 @@ func sendRequest(ctx context.Context, conn *jsonrpc2.Conn, method string, params
114118
result := new([]lsp.TextEdit)
115119
err := conn.Call(ctx, method, params, result)
116120
return result, err
121+
case "textDocument/documentSymbol":
122+
result := new([]DocumentSymbol)
123+
err := conn.Call(ctx, method, params, result)
124+
return result, err
117125
case "window/showMessageRequest":
118126
result := new(lsp.MessageActionItem)
119127
err := conn.Call(ctx, method, params, result)
@@ -144,3 +152,14 @@ type MarkupContent struct {
144152
Kind string `json:"kind"`
145153
Value string `json:"value"`
146154
}
155+
156+
// DocumentSymbol structure according to LSP
157+
type DocumentSymbol struct {
158+
Name string `json:"name"`
159+
Detail string `json:"detail,omitempty"`
160+
Kind lsp.SymbolKind `json:"kind"`
161+
Deprecated bool `json:"deprecated,omitempty"`
162+
Range lsp.Range `json:"range"`
163+
SelectionRange lsp.Range `json:"selectionRange"`
164+
Children []DocumentSymbol `json:"children,omitempty"`
165+
}

0 commit comments

Comments
 (0)