11use std:: ops:: ControlFlow ;
2- use tracing:: { debug , info} ;
2+ use tracing:: { error , info} ;
33
44use 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 ,
910} ;
10- use async_lsp:: { ErrorCode , LanguageClient , LanguageServer , ResponseError } ;
11+ use async_lsp:: { LanguageClient , LanguageServer , ResponseError } ;
1112use futures:: future:: BoxFuture ;
1213
1314use crate :: server:: ServerState ;
@@ -29,7 +30,6 @@ impl LanguageServer for ServerState {
2930 let cversion = version. unwrap_or ( "<unknown>" ) ;
3031
3132 info ! ( "Connected with client {cname} {cversion}" ) ;
32- debug ! ( "Initialize with {params:?}" ) ;
3333
3434 let response = InitializeResult {
3535 capabilities : ServerCapabilities {
@@ -58,39 +58,26 @@ impl LanguageServer for ServerState {
5858 let uri = param. text_document_position_params . text_document . uri ;
5959 let pos = param. text_document_position_params . position ;
6060
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+ }
9481 }
9582
9683 fn definition (
@@ -100,34 +87,20 @@ impl LanguageServer for ServerState {
10087 let uri = param. text_document_position_params . text_document . uri ;
10188 let pos = param. text_document_position_params . position ;
10289
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 ( ) ) ;
12394
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+ } ;
129100
130- Box :: pin ( async move { Ok ( response) } )
101+ Box :: pin ( async move { Ok ( response) } )
102+ }
103+ }
131104 }
132105
133106 fn did_save ( & mut self , _: DidSaveTextDocumentParams ) -> Self :: NotifyResult {
@@ -137,34 +110,45 @@ impl LanguageServer for ServerState {
137110 fn did_open ( & mut self , params : DidOpenTextDocumentParams ) -> Self :: NotifyResult {
138111 let uri = params. text_document . uri ;
139112 let contents = params. text_document . text ;
140- info ! ( "Opened file at: {:}" , uri) ;
113+
114+ info ! ( "opened file at: {uri}" ) ;
141115 self . documents . insert ( uri. clone ( ) , contents. clone ( ) ) ;
142116
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} " ) ;
145119 return ControlFlow :: Continue ( ( ) ) ;
146120 } ;
147121
148- let diagnostics = parsed . collect_parse_errors ( & uri) ;
122+ let diagnostics = tree . collect_parse_errors ( & uri) ;
149123 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" )
151125 }
152126 ControlFlow :: Continue ( ( ) )
153127 }
154128
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+
155138 fn did_change ( & mut self , params : DidChangeTextDocumentParams ) -> Self :: NotifyResult {
156139 let uri = params. text_document . uri ;
157140 let contents = params. content_changes [ 0 ] . text . clone ( ) ;
141+
158142 self . documents . insert ( uri. clone ( ) , contents. clone ( ) ) ;
159143
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} " ) ;
162146 return ControlFlow :: Continue ( ( ) ) ;
163147 } ;
164148
165- let diagnostics = parsed . collect_parse_errors ( & uri) ;
149+ let diagnostics = tree . collect_parse_errors ( & uri) ;
166150 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" )
168152 }
169153 ControlFlow :: Continue ( ( ) )
170154 }
@@ -175,28 +159,14 @@ impl LanguageServer for ServerState {
175159 ) -> BoxFuture < ' static , Result < Option < DocumentSymbolResponse > , Self :: Error > > {
176160 let uri = params. text_document . uri ;
177161
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) ;
195167
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+ }
201171 }
202172}
0 commit comments