Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Workspace Edit ResourceOps #15101

Merged
merged 4 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
105 changes: 101 additions & 4 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
};

use anyhow::Context;

use ide::{
AnnotationConfig, AssistKind, AssistResolveStrategy, Cancellable, FilePosition, FileRange,
HoverAction, HoverGotoTypeData, Query, RangeInfo, ReferenceCategory, Runnable, RunnableKind,
Expand All @@ -20,9 +21,9 @@ use lsp_types::{
CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
CodeLens, CompletionItem, FoldingRange, FoldingRangeParams, HoverContents, InlayHint,
InlayHintParams, Location, LocationLink, Position, PrepareRenameResponse, Range, RenameParams,
SemanticTokensDeltaParams, SemanticTokensFullDeltaResult, SemanticTokensParams,
SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation,
SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit,
ResourceOp, ResourceOperationKind, SemanticTokensDeltaParams, SemanticTokensFullDeltaResult,
SemanticTokensParams, SemanticTokensRangeParams, SemanticTokensRangeResult,
SemanticTokensResult, SymbolInformation, SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit,
};
use project_model::{ManifestPath, ProjectWorkspace, TargetKind};
use serde_json::json;
Expand All @@ -33,7 +34,7 @@ use vfs::{AbsPath, AbsPathBuf, VfsPath};

use crate::{
cargo_target_spec::CargoTargetSpec,
config::{RustfmtConfig, WorkspaceSymbolConfig},
config::{Config, RustfmtConfig, WorkspaceSymbolConfig},
diff::diff,
from_proto,
global_state::{GlobalState, GlobalStateSnapshot},
Expand Down Expand Up @@ -548,6 +549,10 @@ pub(crate) fn handle_will_rename_files(
) -> anyhow::Result<Option<lsp_types::WorkspaceEdit>> {
let _p = profile::span("handle_will_rename_files");

if let Err(err) = resource_ops_supported(&snap.config, &ResourceOperationKind::Rename) {
return Err(err);
}

let source_changes: Vec<SourceChange> = params
.files
.into_iter()
Expand Down Expand Up @@ -1030,7 +1035,28 @@ pub(crate) fn handle_rename(
if !change.file_system_edits.is_empty() && snap.config.will_rename() {
change.source_file_edits.clear();
}

let workspace_edit = to_proto::workspace_edit(&snap, change)?;

if let Some(lsp_types::DocumentChanges::Operations(ops)) =
workspace_edit.document_changes.as_ref()
{
for op in ops {
if let lsp_types::DocumentChangeOperation::Op(doc_change_op) = op {
if let Err(err) = resource_ops_supported(
&snap.config,
match doc_change_op {
ResourceOp::Create(_) => &ResourceOperationKind::Create,
ResourceOp::Rename(_) => &ResourceOperationKind::Rename,
ResourceOp::Delete(_) => &ResourceOperationKind::Delete,
},
alibektas marked this conversation as resolved.
Show resolved Hide resolved
) {
return Err(err);
}
}
}
}

Ok(Some(workspace_edit))
}

Expand Down Expand Up @@ -1137,6 +1163,25 @@ pub(crate) fn handle_code_action(
let resolve_data =
if code_action_resolve_cap { Some((index, params.clone())) } else { None };
let code_action = to_proto::code_action(&snap, assist, resolve_data)?;

// Check if the client supports the necessary `ResourceOperation`s.
if let Some(changes) = &code_action.edit.as_ref().unwrap().document_changes {
alibektas marked this conversation as resolved.
Show resolved Hide resolved
for change in changes {
if let lsp_ext::SnippetDocumentChangeOperation::Op(res_op) = change {
if let Err(err) = resource_ops_supported(
&snap.config,
match res_op {
ResourceOp::Create(_) => &ResourceOperationKind::Create,
ResourceOp::Rename(_) => &ResourceOperationKind::Rename,
ResourceOp::Delete(_) => &ResourceOperationKind::Delete,
},
) {
return Err(err);
}
}
}
}

res.push(code_action)
}

Expand Down Expand Up @@ -1219,6 +1264,26 @@ pub(crate) fn handle_code_action_resolve(
let ca = to_proto::code_action(&snap, assist.clone(), None)?;
code_action.edit = ca.edit;
code_action.command = ca.command;

if let Some(edit) = code_action.edit.as_ref() {
if let Some(changes) = edit.document_changes.as_ref() {
for change in changes {
if let lsp_ext::SnippetDocumentChangeOperation::Op(res_op) = change {
if let Err(err) = resource_ops_supported(
&snap.config,
match res_op {
ResourceOp::Create(_) => &ResourceOperationKind::Create,
ResourceOp::Rename(_) => &ResourceOperationKind::Rename,
ResourceOp::Delete(_) => &ResourceOperationKind::Delete,
},
) {
return Err(err);
}
}
}
}
}

Ok(code_action)
}

Expand Down Expand Up @@ -1990,3 +2055,35 @@ fn to_url(path: VfsPath) -> Option<Url> {
let str_path = path.as_os_str().to_str()?;
Url::from_file_path(str_path).ok()
}

fn resource_ops_supported(config: &Config, kind: &ResourceOperationKind) -> anyhow::Result<()> {
alibektas marked this conversation as resolved.
Show resolved Hide resolved
let ctn = config
.caps()
.workspace
.as_ref()
.unwrap()
.workspace_edit
.as_ref()
.unwrap()
.resource_operations
.as_ref()
.unwrap()
.contains(kind);

if !ctn {
return Err(LspError::new(
ErrorCode::RequestFailed as i32,
format!(
"Client does not support {} capability.",
match kind {
ResourceOperationKind::Create => "create",
ResourceOperationKind::Rename => "rename",
ResourceOperationKind::Delete => "delete",
}
),
)
.into());
}

Ok(())
}
8 changes: 8 additions & 0 deletions crates/rust-analyzer/tests/slow-tests/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ impl<'a> Project<'a> {
relative_pattern_support: None,
},
),
workspace_edit: Some(lsp_types::WorkspaceEditClientCapabilities {
resource_operations: Some(vec![
lsp_types::ResourceOperationKind::Create,
lsp_types::ResourceOperationKind::Delete,
lsp_types::ResourceOperationKind::Rename,
]),
..Default::default()
}),
..Default::default()
}),
text_document: Some(lsp_types::TextDocumentClientCapabilities {
Expand Down