1
1
use std:: ops:: ControlFlow ;
2
- use tracing:: { debug , info} ;
2
+ use tracing:: { error , info} ;
3
3
4
4
use async_lsp:: lsp_types:: {
5
- DidChangeTextDocumentParams , DidOpenTextDocumentParams , DidSaveTextDocumentParams ,
6
- DocumentSymbolParams , DocumentSymbolResponse , GotoDefinitionParams , GotoDefinitionResponse ,
7
- Hover , HoverContents , HoverParams , HoverProviderCapability , InitializeParams , InitializeResult ,
8
- OneOf , ServerCapabilities , ServerInfo , TextDocumentSyncCapability , TextDocumentSyncKind ,
5
+ DidChangeTextDocumentParams , DidCloseTextDocumentParams , DidOpenTextDocumentParams ,
6
+ DidSaveTextDocumentParams , DocumentSymbolParams , DocumentSymbolResponse , GotoDefinitionParams ,
7
+ GotoDefinitionResponse , Hover , HoverContents , HoverParams , HoverProviderCapability ,
8
+ InitializeParams , InitializeResult , OneOf , ServerCapabilities , ServerInfo ,
9
+ TextDocumentSyncCapability , TextDocumentSyncKind ,
9
10
} ;
10
- use async_lsp:: { ErrorCode , LanguageClient , LanguageServer , ResponseError } ;
11
+ use async_lsp:: { LanguageClient , LanguageServer , ResponseError } ;
11
12
use futures:: future:: BoxFuture ;
12
13
13
14
use crate :: server:: ServerState ;
@@ -29,7 +30,6 @@ impl LanguageServer for ServerState {
29
30
let cversion = version. unwrap_or ( "<unknown>" ) ;
30
31
31
32
info ! ( "Connected with client {cname} {cversion}" ) ;
32
- debug ! ( "Initialize with {params:?}" ) ;
33
33
34
34
let response = InitializeResult {
35
35
capabilities : ServerCapabilities {
@@ -58,39 +58,26 @@ impl LanguageServer for ServerState {
58
58
let uri = param. text_document_position_params . text_document . uri ;
59
59
let pos = param. text_document_position_params . position ;
60
60
61
- let Some ( contents) = self . documents . get ( & uri) else {
62
- return Box :: pin ( async move {
63
- Err ( ResponseError :: new (
64
- ErrorCode :: INVALID_REQUEST ,
65
- "uri was never opened" ,
66
- ) )
67
- } ) ;
68
- } ;
69
-
70
- let Some ( parsed) = self . parser . parse ( contents. as_bytes ( ) ) else {
71
- return Box :: pin ( async move {
72
- Err ( ResponseError :: new (
73
- ErrorCode :: REQUEST_FAILED ,
74
- "ts failed to parse contents" ,
75
- ) )
76
- } ) ;
77
- } ;
78
-
79
- let comments = parsed. hover ( & pos, contents. as_bytes ( ) ) ;
80
- info ! ( "Found {} node comments in the document" , comments. len( ) ) ;
81
- let response = match comments. len ( ) {
82
- 0 => None ,
83
- 1 => Some ( Hover {
84
- contents : HoverContents :: Scalar ( comments[ 0 ] . clone ( ) ) ,
85
- range : None ,
86
- } ) ,
87
- 2 .. => Some ( Hover {
88
- contents : HoverContents :: Array ( comments) ,
89
- range : None ,
90
- } ) ,
91
- } ;
92
-
93
- Box :: pin ( async move { Ok ( response) } )
61
+ match self . get_parsed_tree_and_content ( & uri) {
62
+ Err ( e) => Box :: pin ( async move { Err ( e) } ) ,
63
+ Ok ( ( tree, content) ) => {
64
+ let comments = tree. hover ( & pos, content. as_bytes ( ) ) ;
65
+
66
+ let response = match comments. len ( ) {
67
+ 0 => None ,
68
+ 1 => Some ( Hover {
69
+ contents : HoverContents :: Scalar ( comments[ 0 ] . clone ( ) ) ,
70
+ range : None ,
71
+ } ) ,
72
+ 2 .. => Some ( Hover {
73
+ contents : HoverContents :: Array ( comments) ,
74
+ range : None ,
75
+ } ) ,
76
+ } ;
77
+
78
+ Box :: pin ( async move { Ok ( response) } )
79
+ }
80
+ }
94
81
}
95
82
96
83
fn definition (
@@ -100,34 +87,20 @@ impl LanguageServer for ServerState {
100
87
let uri = param. text_document_position_params . text_document . uri ;
101
88
let pos = param. text_document_position_params . position ;
102
89
103
- let Some ( contents) = self . documents . get ( & uri) else {
104
- return Box :: pin ( async move {
105
- Err ( ResponseError :: new (
106
- ErrorCode :: INVALID_REQUEST ,
107
- "uri was never opened" ,
108
- ) )
109
- } ) ;
110
- } ;
111
-
112
- let Some ( parsed) = self . parser . parse ( contents. as_bytes ( ) ) else {
113
- return Box :: pin ( async move {
114
- Err ( ResponseError :: new (
115
- ErrorCode :: REQUEST_FAILED ,
116
- "ts failed to parse contents" ,
117
- ) )
118
- } ) ;
119
- } ;
120
-
121
- let locations = parsed. definition ( & pos, & uri, contents. as_bytes ( ) ) ;
122
- info ! ( "Found {} matching nodes in the document" , locations. len( ) ) ;
90
+ match self . get_parsed_tree_and_content ( & uri) {
91
+ Err ( e) => Box :: pin ( async move { Err ( e) } ) ,
92
+ Ok ( ( tree, content) ) => {
93
+ let locations = tree. definition ( & pos, & uri, content. as_bytes ( ) ) ;
123
94
124
- let response = match locations. len ( ) {
125
- 0 => None ,
126
- 1 => Some ( GotoDefinitionResponse :: Scalar ( locations[ 0 ] . clone ( ) ) ) ,
127
- 2 .. => Some ( GotoDefinitionResponse :: Array ( locations) ) ,
128
- } ;
95
+ let response = match locations. len ( ) {
96
+ 0 => None ,
97
+ 1 => Some ( GotoDefinitionResponse :: Scalar ( locations[ 0 ] . clone ( ) ) ) ,
98
+ 2 .. => Some ( GotoDefinitionResponse :: Array ( locations) ) ,
99
+ } ;
129
100
130
- Box :: pin ( async move { Ok ( response) } )
101
+ Box :: pin ( async move { Ok ( response) } )
102
+ }
103
+ }
131
104
}
132
105
133
106
fn did_save ( & mut self , _: DidSaveTextDocumentParams ) -> Self :: NotifyResult {
@@ -137,34 +110,45 @@ impl LanguageServer for ServerState {
137
110
fn did_open ( & mut self , params : DidOpenTextDocumentParams ) -> Self :: NotifyResult {
138
111
let uri = params. text_document . uri ;
139
112
let contents = params. text_document . text ;
140
- info ! ( "Opened file at: {:}" , uri) ;
113
+
114
+ info ! ( "opened file at: {uri}" ) ;
141
115
self . documents . insert ( uri. clone ( ) , contents. clone ( ) ) ;
142
116
143
- let Some ( parsed ) = self . parser . parse ( contents. as_bytes ( ) ) else {
144
- tracing :: error!( "failed to parse content" ) ;
117
+ let Some ( tree ) = self . parser . parse ( contents. as_bytes ( ) ) else {
118
+ error ! ( "failed to parse content at {uri} " ) ;
145
119
return ControlFlow :: Continue ( ( ) ) ;
146
120
} ;
147
121
148
- let diagnostics = parsed . collect_parse_errors ( & uri) ;
122
+ let diagnostics = tree . collect_parse_errors ( & uri) ;
149
123
if let Err ( e) = self . client . publish_diagnostics ( diagnostics) {
150
- tracing :: error!( "failed to publish diagnostics. {:?}" , e )
124
+ error ! ( error=%e , "failed to publish diagnostics" )
151
125
}
152
126
ControlFlow :: Continue ( ( ) )
153
127
}
154
128
129
+ fn did_close ( & mut self , params : DidCloseTextDocumentParams ) -> Self :: NotifyResult {
130
+ let uri = params. text_document . uri ;
131
+
132
+ info ! ( "closed file at {uri}" ) ;
133
+ self . documents . remove ( & uri) ;
134
+
135
+ ControlFlow :: Continue ( ( ) )
136
+ }
137
+
155
138
fn did_change ( & mut self , params : DidChangeTextDocumentParams ) -> Self :: NotifyResult {
156
139
let uri = params. text_document . uri ;
157
140
let contents = params. content_changes [ 0 ] . text . clone ( ) ;
141
+
158
142
self . documents . insert ( uri. clone ( ) , contents. clone ( ) ) ;
159
143
160
- let Some ( parsed ) = self . parser . parse ( contents. as_bytes ( ) ) else {
161
- tracing :: error!( "failed to parse content" ) ;
144
+ let Some ( tree ) = self . parser . parse ( contents. as_bytes ( ) ) else {
145
+ error ! ( "failed to parse content at {uri} " ) ;
162
146
return ControlFlow :: Continue ( ( ) ) ;
163
147
} ;
164
148
165
- let diagnostics = parsed . collect_parse_errors ( & uri) ;
149
+ let diagnostics = tree . collect_parse_errors ( & uri) ;
166
150
if let Err ( e) = self . client . publish_diagnostics ( diagnostics) {
167
- tracing :: error!( "failed to publish diagnostics. {:?}" , e )
151
+ error ! ( error=%e , "failed to publish diagnostics" )
168
152
}
169
153
ControlFlow :: Continue ( ( ) )
170
154
}
@@ -175,28 +159,14 @@ impl LanguageServer for ServerState {
175
159
) -> BoxFuture < ' static , Result < Option < DocumentSymbolResponse > , Self :: Error > > {
176
160
let uri = params. text_document . uri ;
177
161
178
- let Some ( contents) = self . documents . get ( & uri) else {
179
- return Box :: pin ( async move {
180
- Err ( ResponseError :: new (
181
- ErrorCode :: INVALID_REQUEST ,
182
- "uri was never opened" ,
183
- ) )
184
- } ) ;
185
- } ;
186
-
187
- let Some ( parsed) = self . parser . parse ( contents. as_bytes ( ) ) else {
188
- return Box :: pin ( async move {
189
- Err ( ResponseError :: new (
190
- ErrorCode :: REQUEST_FAILED ,
191
- "ts failed to parse contents" ,
192
- ) )
193
- } ) ;
194
- } ;
162
+ match self . get_parsed_tree_and_content ( & uri) {
163
+ Err ( e) => Box :: pin ( async move { Err ( e) } ) ,
164
+ Ok ( ( tree, content) ) => {
165
+ let locations = tree. find_document_locations ( content. as_bytes ( ) ) ;
166
+ let response = DocumentSymbolResponse :: Nested ( locations) ;
195
167
196
- let locations = parsed. find_document_locations ( contents. as_bytes ( ) ) ;
197
-
198
- let response = DocumentSymbolResponse :: Nested ( locations) ;
199
-
200
- Box :: pin ( async move { Ok ( Some ( response) ) } )
168
+ Box :: pin ( async move { Ok ( Some ( response) ) } )
169
+ }
170
+ }
201
171
}
202
172
}
0 commit comments