11use djls_source:: PositionEncoding ;
2- use tower_lsp_server:: lsp_types:: InitializeParams ;
3- use tower_lsp_server:: lsp_types:: PositionEncodingKind ;
2+ use tower_lsp_server:: lsp_types;
43
54/// Negotiate the best encoding with the client based on their capabilities.
65/// Prefers UTF-8 > UTF-32 > UTF-16 for performance reasons.
7- pub fn negotiate_position_encoding ( params : & InitializeParams ) -> PositionEncoding {
8- let client_encodings: & [ PositionEncodingKind ] = params
6+ pub fn negotiate_position_encoding ( params : & lsp_types :: InitializeParams ) -> PositionEncoding {
7+ let client_encodings: & [ lsp_types :: PositionEncodingKind ] = params
98 . capabilities
109 . general
1110 . as_ref ( )
1211 . and_then ( |general| general. position_encodings . as_ref ( ) )
1312 . map_or ( & [ ] , |encodings| encodings. as_slice ( ) ) ;
1413
15- // Try to find the best encoding in preference order
1614 for preferred in [
1715 PositionEncoding :: Utf8 ,
1816 PositionEncoding :: Utf32 ,
@@ -30,18 +28,19 @@ pub fn negotiate_position_encoding(params: &InitializeParams) -> PositionEncodin
3028 PositionEncoding :: Utf16
3129}
3230
33- // Helper functions to convert between LSP types and our PositionEncoding
3431#[ must_use]
35- pub fn position_encoding_to_lsp ( encoding : PositionEncoding ) -> PositionEncodingKind {
32+ pub fn position_encoding_to_lsp ( encoding : PositionEncoding ) -> lsp_types :: PositionEncodingKind {
3633 match encoding {
37- PositionEncoding :: Utf8 => PositionEncodingKind :: new ( "utf-8" ) ,
38- PositionEncoding :: Utf16 => PositionEncodingKind :: new ( "utf-16" ) ,
39- PositionEncoding :: Utf32 => PositionEncodingKind :: new ( "utf-32" ) ,
34+ PositionEncoding :: Utf8 => lsp_types :: PositionEncodingKind :: new ( "utf-8" ) ,
35+ PositionEncoding :: Utf16 => lsp_types :: PositionEncodingKind :: new ( "utf-16" ) ,
36+ PositionEncoding :: Utf32 => lsp_types :: PositionEncodingKind :: new ( "utf-32" ) ,
4037 }
4138}
4239
4340#[ must_use]
44- pub fn position_encoding_from_lsp ( kind : & PositionEncodingKind ) -> Option < PositionEncoding > {
41+ pub fn position_encoding_from_lsp (
42+ kind : & lsp_types:: PositionEncodingKind ,
43+ ) -> Option < PositionEncoding > {
4544 match kind. as_str ( ) {
4645 "utf-8" => Some ( PositionEncoding :: Utf8 ) ,
4746 "utf-16" => Some ( PositionEncoding :: Utf16 ) ,
@@ -61,21 +60,21 @@ mod tests {
6160 fn test_lsp_type_conversions ( ) {
6261 // position_encoding_from_lsp for valid encodings
6362 assert_eq ! (
64- position_encoding_from_lsp( & PositionEncodingKind :: new( "utf-8" ) ) ,
63+ position_encoding_from_lsp( & lsp_types :: PositionEncodingKind :: new( "utf-8" ) ) ,
6564 Some ( PositionEncoding :: Utf8 )
6665 ) ;
6766 assert_eq ! (
68- position_encoding_from_lsp( & PositionEncodingKind :: new( "utf-16" ) ) ,
67+ position_encoding_from_lsp( & lsp_types :: PositionEncodingKind :: new( "utf-16" ) ) ,
6968 Some ( PositionEncoding :: Utf16 )
7069 ) ;
7170 assert_eq ! (
72- position_encoding_from_lsp( & PositionEncodingKind :: new( "utf-32" ) ) ,
71+ position_encoding_from_lsp( & lsp_types :: PositionEncodingKind :: new( "utf-32" ) ) ,
7372 Some ( PositionEncoding :: Utf32 )
7473 ) ;
7574
7675 // Invalid encoding returns None
7776 assert_eq ! (
78- position_encoding_from_lsp( & PositionEncodingKind :: new( "unknown" ) ) ,
77+ position_encoding_from_lsp( & lsp_types :: PositionEncodingKind :: new( "unknown" ) ) ,
7978 None
8079 ) ;
8180
@@ -96,13 +95,13 @@ mod tests {
9695
9796 #[ test]
9897 fn test_negotiate_prefers_utf8_when_all_available ( ) {
99- let params = InitializeParams {
98+ let params = lsp_types :: InitializeParams {
10099 capabilities : ClientCapabilities {
101100 general : Some ( GeneralClientCapabilities {
102101 position_encodings : Some ( vec ! [
103- PositionEncodingKind :: new( "utf-16" ) ,
104- PositionEncodingKind :: new( "utf-8" ) ,
105- PositionEncodingKind :: new( "utf-32" ) ,
102+ lsp_types :: PositionEncodingKind :: new( "utf-16" ) ,
103+ lsp_types :: PositionEncodingKind :: new( "utf-8" ) ,
104+ lsp_types :: PositionEncodingKind :: new( "utf-32" ) ,
106105 ] ) ,
107106 ..Default :: default ( )
108107 } ) ,
@@ -116,12 +115,12 @@ mod tests {
116115
117116 #[ test]
118117 fn test_negotiate_prefers_utf32_over_utf16 ( ) {
119- let params = InitializeParams {
118+ let params = lsp_types :: InitializeParams {
120119 capabilities : ClientCapabilities {
121120 general : Some ( GeneralClientCapabilities {
122121 position_encodings : Some ( vec ! [
123- PositionEncodingKind :: new( "utf-16" ) ,
124- PositionEncodingKind :: new( "utf-32" ) ,
122+ lsp_types :: PositionEncodingKind :: new( "utf-16" ) ,
123+ lsp_types :: PositionEncodingKind :: new( "utf-32" ) ,
125124 ] ) ,
126125 ..Default :: default ( )
127126 } ) ,
@@ -138,10 +137,10 @@ mod tests {
138137
139138 #[ test]
140139 fn test_negotiate_accepts_utf16_when_only_option ( ) {
141- let params = InitializeParams {
140+ let params = lsp_types :: InitializeParams {
142141 capabilities : ClientCapabilities {
143142 general : Some ( GeneralClientCapabilities {
144- position_encodings : Some ( vec ! [ PositionEncodingKind :: new( "utf-16" ) ] ) ,
143+ position_encodings : Some ( vec ! [ lsp_types :: PositionEncodingKind :: new( "utf-16" ) ] ) ,
145144 ..Default :: default ( )
146145 } ) ,
147146 ..Default :: default ( )
@@ -157,7 +156,7 @@ mod tests {
157156
158157 #[ test]
159158 fn test_negotiate_fallback_with_empty_encodings ( ) {
160- let params = InitializeParams {
159+ let params = lsp_types :: InitializeParams {
161160 capabilities : ClientCapabilities {
162161 general : Some ( GeneralClientCapabilities {
163162 position_encodings : Some ( vec ! [ ] ) ,
@@ -176,7 +175,7 @@ mod tests {
176175
177176 #[ test]
178177 fn test_negotiate_fallback_with_no_capabilities ( ) {
179- let params = InitializeParams :: default ( ) ;
178+ let params = lsp_types :: InitializeParams :: default ( ) ;
180179 assert_eq ! (
181180 negotiate_position_encoding( & params) ,
182181 PositionEncoding :: Utf16
@@ -185,12 +184,12 @@ mod tests {
185184
186185 #[ test]
187186 fn test_negotiate_fallback_with_unknown_encodings ( ) {
188- let params = InitializeParams {
187+ let params = lsp_types :: InitializeParams {
189188 capabilities : ClientCapabilities {
190189 general : Some ( GeneralClientCapabilities {
191190 position_encodings : Some ( vec ! [
192- PositionEncodingKind :: new( "utf-7" ) ,
193- PositionEncodingKind :: new( "ascii" ) ,
191+ lsp_types :: PositionEncodingKind :: new( "utf-7" ) ,
192+ lsp_types :: PositionEncodingKind :: new( "ascii" ) ,
194193 ] ) ,
195194 ..Default :: default ( )
196195 } ) ,
0 commit comments