Skip to content

Commit 3f4e26a

Browse files
committed
state handling in hover
1 parent 4d83123 commit 3f4e26a

File tree

4 files changed

+35
-41
lines changed

4 files changed

+35
-41
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
main
2+
logs.txt

analysis/state.go

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package analysis
22

3+
import (
4+
"fmt"
5+
"lsp-go/lsp"
6+
)
7+
38
// mapping of all the documents
49
type State struct {
510
// map of document uris to their content
@@ -20,3 +25,16 @@ func (state *State) OpenDocument(path string, contents string) {
2025
func (state *State) UpdateDocument(path string, contents string) {
2126
state.Documents[path] = contents
2227
}
28+
29+
func (state *State) Hover(id int, path string) lsp.HoverResponse {
30+
fileContent := state.Documents[path]
31+
return lsp.HoverResponse{
32+
Response: lsp.Response{
33+
RPC: "2.0",
34+
ID: &id,
35+
},
36+
Result: lsp.HoverResult{
37+
Contents: fmt.Sprintf("File at %s with %d characters", path, len(fileContent)),
38+
},
39+
}
40+
}

logs.txt

+15-31
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,17 @@
1-
[custom-lsp]2024/10/28 03:14:39 main.go:16: logger initiated bruv..
2-
[custom-lsp]2024/10/28 03:14:39 main.go:18: hiii
3-
[custom-lsp]2024/10/28 03:14:39 main.go:27: attached splitter
4-
[custom-lsp]2024/10/28 03:14:39 main.go:29: parsing buffer... 1
5-
[custom-lsp]2024/10/28 03:14:39 main.go:50: Received message with method initialize
6-
[custom-lsp]2024/10/28 03:14:39 main.go:58: [INITIALIZE] Connected to client Neovim with version 0.9.5
7-
[custom-lsp]2024/10/28 03:14:39 main.go:107: responded back to client with Content-Length: 156
1+
[custom-lsp]2024/10/28 03:28:16 main.go:16: logger initiated bruv..
2+
[custom-lsp]2024/10/28 03:28:16 main.go:18: hiii
3+
[custom-lsp]2024/10/28 03:28:16 main.go:27: attached splitter
4+
[custom-lsp]2024/10/28 03:28:16 main.go:29: parsing buffer... 1
5+
[custom-lsp]2024/10/28 03:28:16 main.go:50: Received message with method initialize
6+
[custom-lsp]2024/10/28 03:28:16 main.go:58: [INITIALIZE] Connected to client Neovim with version 0.9.5
7+
[custom-lsp]2024/10/28 03:28:16 main.go:98: responded back to client with Content-Length: 156
88

99
{"jsonrpc":"2.0","id":1,"result":{"capabilities":{"textDocumentSync":1,"hoverProvider":true},"serverInfo":{"name":"custom-lsp","version":"0.0.0.0.beta-1"}}}
10-
[custom-lsp]2024/10/28 03:14:39 main.go:41: [progress] Buffer parsed for [1]th time
11-
[custom-lsp]2024/10/28 03:14:40 main.go:29: parsing buffer... 2
12-
[custom-lsp]2024/10/28 03:14:40 main.go:50: Received message with method initialized
13-
[custom-lsp]2024/10/28 03:14:40 main.go:41: [progress] Buffer parsed for [2]th time
14-
[custom-lsp]2024/10/28 03:14:40 main.go:29: parsing buffer... 3
15-
[custom-lsp]2024/10/28 03:14:40 main.go:50: Received message with method textDocument/didOpen
16-
[custom-lsp]2024/10/28 03:14:40 main.go:69: [textdoc/didOpen] OPENED loaded file at: [ file:///Users/ayush/Desktop/learn/lsp-go/readme.md ]
17-
[custom-lsp]2024/10/28 03:14:40 main.go:41: [progress] Buffer [custom-lsp]2024/10/28 03:15:08 main.go:28: parsing buffer... 4
18-
[custom-lsp]2024/10/28 03:15:08 main.go:46: Received message with method shutdown
19-
[custom-lsp]2024/10/28 03:15:08 main.go:37: [progress] Buffer parsed for [4]th time
20-
[custom-lsp]2024/10/28 03:15:08 main.go:40: finished scanning
21-
Desktop/learn/lsp-go/readme.md ]
22-
[custom-lsp]2024/10/28 03:14:41 main.go:41: [progress] Buffer parsed for [4]th time
23-
[custom-lsp]2024/10/28 03:14:42 main.go:29: parsing buffer... 5
24-
[custom-lsp]2024/10/28 03:14:42 main.go:50: Received message with method textDocument/didChange
25-
[custom-lsp]2024/10/28 03:14:42 main.go:77: [textdoc/didChange] UPDATED loaded file at: [ file:///Users/ayush/Desktop/learn/lsp-go/readme.md ]
26-
[custom-lsp]2024/10/28 03:14:42 main.go:41: [progress] Buffer parsed for [5]th time
27-
[custom-lsp]2024/10/28 03:14:43 main.go:29: parsing buffer... 6
28-
[custom-lsp]2024/10/28 03:14:43 main.go:50: Received message with method textDocument/hover
29-
[custom-lsp]2024/10/28 03:14:43 main.go:88: [textdoc/hover] HOVER loaded file at: [ file:///Users/ayush/Desktop/learn/lsp-go/readme.md ]
30-
[custom-lsp]2024/10/28 03:14:43 main.go:107: responded back to client with Content-Length: 71
31-
32-
{"jsonrpc":"2.0","id":2,"result":{"contents":"Hello from custom-lsp!"}}
33-
[custom-lsp]2024/10/28 03:14:43 main.go:41: [progress] Buffer parsed for [6]th time
10+
[custom-lsp]2024/10/28 03:28:16 main.go:41: [progress] Buffer parsed for [1]th time
11+
[custom-lsp]2024/10/28 03:28:17 main.go:29: parsing buffer... 2
12+
[custom-lsp]2024/10/28 03:28:17 main.go:50: Received message with method initialized
13+
[custom-lsp]2024/10/28 03:28:17 main.go:41: [progress] Buffer parsed for [2]th time
14+
[custom-lsp]2024/10/28 03:28:21 main.go:29: parsing buffer... 3
15+
[custom-lsp]2024/10/28 03:28:21 main.go:50: Received message with method shutdown
16+
[custom-lsp]2024/10/28 03:28:21 main.go:41: [progress] Buffer parsed for [3]th time
17+
[custom-lsp]2024/10/28 03:28:21 main.go:44: finished scanning

main.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,7 @@ func handleMessage(logger *log.Logger, writer io.Writer, state analysis.State, m
8787
}
8888
logger.Printf("[textdoc/hover] HOVER loaded file at: [ %s ]", request.Params.TextDocument.Uri)
8989
// prepare resposne
90-
response := lsp.HoverResponse{
91-
Response: lsp.Response{
92-
RPC: "2.0",
93-
ID: &request.ID,
94-
},
95-
Result: lsp.HoverResult{
96-
Contents: "Hello from custom-lsp!",
97-
},
98-
}
99-
// write it back to the stream
90+
response := state.Hover(request.ID, request.Params.TextDocument.Uri) // write it back to the stream
10091
writeResponse(logger, response, writer)
10192
}
10293
}

0 commit comments

Comments
 (0)