@@ -8,10 +8,11 @@ use std::{
8
8
} ;
9
9
10
10
use anyhow:: Context ;
11
+
11
12
use ide:: {
12
13
AnnotationConfig , AssistKind , AssistResolveStrategy , Cancellable , FilePosition , FileRange ,
13
- FileSystemEdit , HoverAction , HoverGotoTypeData , Query , RangeInfo , ReferenceCategory , Runnable ,
14
- RunnableKind , SingleResolve , SourceChange , TextEdit ,
14
+ HoverAction , HoverGotoTypeData , Query , RangeInfo , ReferenceCategory , Runnable , RunnableKind ,
15
+ SingleResolve , SourceChange , TextEdit ,
15
16
} ;
16
17
use ide_db:: SymbolKind ;
17
18
use lsp_server:: ErrorCode ;
@@ -33,7 +34,7 @@ use vfs::{AbsPath, AbsPathBuf, VfsPath};
33
34
34
35
use crate :: {
35
36
cargo_target_spec:: CargoTargetSpec ,
36
- config:: { RustfmtConfig , WorkspaceSymbolConfig } ,
37
+ config:: { Config , RustfmtConfig , WorkspaceSymbolConfig } ,
37
38
diff:: diff,
38
39
from_proto,
39
40
global_state:: { GlobalState , GlobalStateSnapshot } ,
@@ -548,12 +549,8 @@ pub(crate) fn handle_will_rename_files(
548
549
) -> anyhow:: Result < Option < lsp_types:: WorkspaceEdit > > {
549
550
let _p = profile:: span ( "handle_will_rename_files" ) ;
550
551
551
- if !resource_ops_supported ( & snap, & ResourceOperationKind :: Rename ) {
552
- return Err ( LspError :: new (
553
- ErrorCode :: RequestFailed as i32 ,
554
- "Client does not support rename capability." . to_owned ( ) ,
555
- )
556
- . into ( ) ) ;
552
+ if let Err ( err) = resource_ops_supported ( & snap. config , & ResourceOperationKind :: Rename ) {
553
+ return Err ( err) ;
557
554
}
558
555
559
556
let source_changes: Vec < SourceChange > = params
@@ -1037,33 +1034,29 @@ pub(crate) fn handle_rename(
1037
1034
// See https://github.com/microsoft/vscode-languageserver-node/issues/752 for more info
1038
1035
if !change. file_system_edits . is_empty ( ) && snap. config . will_rename ( ) {
1039
1036
change. source_file_edits . clear ( ) ;
1040
- } else {
1041
- for edit in & change. file_system_edits {
1042
- match edit {
1043
- & FileSystemEdit :: CreateFile { .. }
1044
- if !resource_ops_supported ( & snap, & ResourceOperationKind :: Create ) =>
1045
- {
1046
- return Err ( LspError :: new (
1047
- ErrorCode :: RequestFailed as i32 ,
1048
- "Client does not support create capability." . to_owned ( ) ,
1049
- )
1050
- . into ( ) )
1051
- }
1052
- & FileSystemEdit :: MoveFile { .. } | & FileSystemEdit :: MoveDir { .. }
1053
- if !resource_ops_supported ( & snap, & ResourceOperationKind :: Rename ) =>
1054
- {
1055
- return Err ( LspError :: new (
1056
- ErrorCode :: RequestFailed as i32 ,
1057
- "Client does not support move/rename capability." . to_owned ( ) ,
1058
- )
1059
- . into ( ) )
1037
+ }
1038
+
1039
+ let workspace_edit = to_proto:: workspace_edit ( & snap, change) ?;
1040
+
1041
+ if let Some ( lsp_types:: DocumentChanges :: Operations ( ops) ) =
1042
+ workspace_edit. document_changes . as_ref ( )
1043
+ {
1044
+ for op in ops {
1045
+ if let lsp_types:: DocumentChangeOperation :: Op ( doc_change_op) = op {
1046
+ if let Err ( err) = resource_ops_supported (
1047
+ & snap. config ,
1048
+ match doc_change_op {
1049
+ ResourceOp :: Create ( _) => & ResourceOperationKind :: Create ,
1050
+ ResourceOp :: Rename ( _) => & ResourceOperationKind :: Rename ,
1051
+ ResourceOp :: Delete ( _) => & ResourceOperationKind :: Delete ,
1052
+ } ,
1053
+ ) {
1054
+ return Err ( err) ;
1060
1055
}
1061
- _ => ( ) ,
1062
1056
}
1063
1057
}
1064
1058
}
1065
1059
1066
- let workspace_edit = to_proto:: workspace_edit ( & snap, change) ?;
1067
1060
Ok ( Some ( workspace_edit) )
1068
1061
}
1069
1062
@@ -1174,35 +1167,17 @@ pub(crate) fn handle_code_action(
1174
1167
// Check if the client supports the necessary `ResourceOperation`s.
1175
1168
if let Some ( changes) = & code_action. edit . as_ref ( ) . unwrap ( ) . document_changes {
1176
1169
for change in changes {
1177
- match change {
1178
- lsp_ext:: SnippetDocumentChangeOperation :: Op ( ResourceOp :: Create ( _) )
1179
- if !resource_ops_supported ( & snap, & ResourceOperationKind :: Create ) =>
1180
- {
1181
- return Err ( LspError :: new (
1182
- ErrorCode :: RequestFailed as i32 ,
1183
- "Client does not support create capability." . to_owned ( ) ,
1184
- )
1185
- . into ( ) ) ;
1186
- }
1187
- lsp_ext:: SnippetDocumentChangeOperation :: Op ( ResourceOp :: Rename ( _) )
1188
- if !resource_ops_supported ( & snap, & ResourceOperationKind :: Rename ) =>
1189
- {
1190
- return Err ( LspError :: new (
1191
- ErrorCode :: RequestFailed as i32 ,
1192
- "Client does not support rename capability." . to_owned ( ) ,
1193
- )
1194
- . into ( ) ) ;
1195
- }
1196
- lsp_ext:: SnippetDocumentChangeOperation :: Op ( ResourceOp :: Delete ( _) )
1197
- if !resource_ops_supported ( & snap, & ResourceOperationKind :: Delete ) =>
1198
- {
1199
- return Err ( LspError :: new (
1200
- ErrorCode :: RequestFailed as i32 ,
1201
- "Client does not support delete capability." . to_owned ( ) ,
1202
- )
1203
- . into ( ) ) ;
1170
+ if let lsp_ext:: SnippetDocumentChangeOperation :: Op ( res_op) = change {
1171
+ if let Err ( err) = resource_ops_supported (
1172
+ & snap. config ,
1173
+ match res_op {
1174
+ ResourceOp :: Create ( _) => & ResourceOperationKind :: Create ,
1175
+ ResourceOp :: Rename ( _) => & ResourceOperationKind :: Rename ,
1176
+ ResourceOp :: Delete ( _) => & ResourceOperationKind :: Delete ,
1177
+ } ,
1178
+ ) {
1179
+ return Err ( err) ;
1204
1180
}
1205
- _ => ( ) ,
1206
1181
}
1207
1182
}
1208
1183
}
@@ -1293,35 +1268,17 @@ pub(crate) fn handle_code_action_resolve(
1293
1268
if let Some ( edit) = code_action. edit . as_ref ( ) {
1294
1269
if let Some ( changes) = edit. document_changes . as_ref ( ) {
1295
1270
for change in changes {
1296
- match change {
1297
- lsp_ext:: SnippetDocumentChangeOperation :: Op ( ResourceOp :: Create ( _) )
1298
- if !resource_ops_supported ( & snap, & ResourceOperationKind :: Create ) =>
1299
- {
1300
- return Err ( LspError :: new (
1301
- ErrorCode :: RequestFailed as i32 ,
1302
- "Client does not support create capability." . to_owned ( ) ,
1303
- )
1304
- . into ( ) ) ;
1271
+ if let lsp_ext:: SnippetDocumentChangeOperation :: Op ( res_op) = change {
1272
+ if let Err ( err) = resource_ops_supported (
1273
+ & snap. config ,
1274
+ match res_op {
1275
+ ResourceOp :: Create ( _) => & ResourceOperationKind :: Create ,
1276
+ ResourceOp :: Rename ( _) => & ResourceOperationKind :: Rename ,
1277
+ ResourceOp :: Delete ( _) => & ResourceOperationKind :: Delete ,
1278
+ } ,
1279
+ ) {
1280
+ return Err ( err) ;
1305
1281
}
1306
- lsp_ext:: SnippetDocumentChangeOperation :: Op ( ResourceOp :: Rename ( _) )
1307
- if !resource_ops_supported ( & snap, & ResourceOperationKind :: Rename ) =>
1308
- {
1309
- return Err ( LspError :: new (
1310
- ErrorCode :: RequestFailed as i32 ,
1311
- "Client does not support rename capability." . to_owned ( ) ,
1312
- )
1313
- . into ( ) ) ;
1314
- }
1315
- lsp_ext:: SnippetDocumentChangeOperation :: Op ( ResourceOp :: Delete ( _) )
1316
- if !resource_ops_supported ( & snap, & ResourceOperationKind :: Delete ) =>
1317
- {
1318
- return Err ( LspError :: new (
1319
- ErrorCode :: RequestFailed as i32 ,
1320
- "Client does not support delete capability." . to_owned ( ) ,
1321
- )
1322
- . into ( ) ) ;
1323
- }
1324
- _ => ( ) ,
1325
1282
}
1326
1283
}
1327
1284
}
@@ -2099,9 +2056,8 @@ fn to_url(path: VfsPath) -> Option<Url> {
2099
2056
Url :: from_file_path ( str_path) . ok ( )
2100
2057
}
2101
2058
2102
- fn resource_ops_supported ( snap : & GlobalStateSnapshot , kind : & ResourceOperationKind ) -> bool {
2103
- snap. config
2104
- . as_ref ( )
2059
+ fn resource_ops_supported ( config : & Config , kind : & ResourceOperationKind ) -> anyhow:: Result < ( ) > {
2060
+ let ctn = config
2105
2061
. caps ( )
2106
2062
. workspace
2107
2063
. as_ref ( )
@@ -2112,5 +2068,22 @@ fn resource_ops_supported(snap: &GlobalStateSnapshot, kind: &ResourceOperationKi
2112
2068
. resource_operations
2113
2069
. as_ref ( )
2114
2070
. unwrap ( )
2115
- . contains ( kind)
2071
+ . contains ( kind) ;
2072
+
2073
+ if !ctn {
2074
+ return Err ( LspError :: new (
2075
+ ErrorCode :: RequestFailed as i32 ,
2076
+ format ! (
2077
+ "Client does not support {} capability." ,
2078
+ match kind {
2079
+ ResourceOperationKind :: Create => "create" ,
2080
+ ResourceOperationKind :: Rename => "rename" ,
2081
+ ResourceOperationKind :: Delete => "delete" ,
2082
+ }
2083
+ ) ,
2084
+ )
2085
+ . into ( ) ) ;
2086
+ }
2087
+
2088
+ Ok ( ( ) )
2116
2089
}
0 commit comments