From cd2ebb52246b2adb623d6deaefd8068692899694 Mon Sep 17 00:00:00 2001 From: sofia Date: Sun, 3 Aug 2025 21:45:48 +0300 Subject: [PATCH] Add refactoring --- reid-lsp/src/main.rs | 50 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/reid-lsp/src/main.rs b/reid-lsp/src/main.rs index 9bef85c..5afb303 100644 --- a/reid-lsp/src/main.rs +++ b/reid-lsp/src/main.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::path::PathBuf; use dashmap::DashMap; @@ -10,10 +11,10 @@ use tower_lsp::lsp_types::{ DidChangeTextDocumentParams, DidOpenTextDocumentParams, DocumentFilter, GotoDefinitionParams, GotoDefinitionResponse, Hover, HoverContents, HoverParams, HoverProviderCapability, InitializeParams, InitializeResult, InitializedParams, Location, MarkupContent, MarkupKind, MessageType, OneOf, Range, - ReferenceParams, SemanticToken, SemanticTokensLegend, SemanticTokensOptions, SemanticTokensParams, + ReferenceParams, RenameParams, SemanticToken, SemanticTokensLegend, SemanticTokensOptions, SemanticTokensParams, SemanticTokensResult, SemanticTokensServerCapabilities, ServerCapabilities, TextDocumentItem, TextDocumentRegistrationOptions, TextDocumentSyncCapability, TextDocumentSyncKind, TextDocumentSyncOptions, - WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities, + TextEdit, WorkspaceEdit, WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities, }; use tower_lsp::{Client, LanguageServer, LspService, Server, jsonrpc}; @@ -76,6 +77,7 @@ impl LanguageServer for Backend { )), definition_provider: Some(OneOf::Left(true)), references_provider: Some(OneOf::Left(true)), + rename_provider: Some(OneOf::Left(true)), ..Default::default() }; Ok(InitializeResult { @@ -313,6 +315,50 @@ impl LanguageServer for Backend { Ok(None) } } + + async fn rename(&self, params: RenameParams) -> jsonrpc::Result> { + let path = PathBuf::from(params.text_document_position.text_document.uri.path()); + let file_name = path.file_name().unwrap().to_str().unwrap().to_owned(); + let analysis = self.analysis.get(&file_name); + let position = params.text_document_position.position; + + if let Some(analysis) = &analysis { + let token = analysis.tokens.iter().enumerate().find(|(_, tok)| { + tok.position.1 == position.line + 1 + && (tok.position.0 <= position.character + 1 + && (tok.position.0 + tok.token.len() as u32) > position.character + 1) + }); + if let Some(token) = token { + let tokens = analysis.find_references(token.0).map(|symbols| { + symbols + .iter() + .map(|symbol_id| analysis.state.symbol_to_token.get(&symbol_id).cloned().unwrap()) + .collect::>() + }); + let mut edits = Vec::new(); + if let Some(tokens) = tokens { + for token_idx in tokens { + let token = analysis.tokens.get(token_idx).unwrap(); + edits.push(TextEdit { + range: token_to_range(token), + new_text: params.new_name.clone(), + }); + } + } + let mut changes = HashMap::new(); + changes.insert(params.text_document_position.text_document.uri, edits); + Ok(Some(WorkspaceEdit { + changes: Some(changes), + document_changes: None, + change_annotations: None, + })) + } else { + Ok(None) + } + } else { + Ok(None) + } + } } fn token_to_range(token: &FullToken) -> lsp_types::Range {