Skip to content

Commit

Permalink
feat: add rename capability (#14)
Browse files Browse the repository at this point in the history
- [x] Add unit test for `rename_kind`
- [x] Test renaming via neovim client

---------

Co-authored-by: mohammadkhan <[email protected]>
  • Loading branch information
coder3101 and asharkhan3101 authored Jul 22, 2024
1 parent e72ce0f commit fec7bfb
Show file tree
Hide file tree
Showing 3 changed files with 343 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ A Language Server for **proto3** files. It uses tree-sitter parser for all opera
- [x] Go to definition
- [x] Diagnostics
- [x] Document Symbols for message and enums
- [x] Rename message, enum and rpc
- [x] Completion for proto3 keywords

## Installation

Expand Down
46 changes: 44 additions & 2 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use async_lsp::lsp_types::{
DidChangeTextDocumentParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams,
DidSaveTextDocumentParams, DocumentSymbolParams, DocumentSymbolResponse, GotoDefinitionParams,
GotoDefinitionResponse, Hover, HoverContents, HoverParams, HoverProviderCapability,
InitializeParams, InitializeResult, OneOf, ServerCapabilities, ServerInfo,
TextDocumentSyncCapability, TextDocumentSyncKind,
InitializeParams, InitializeResult, OneOf, PrepareRenameResponse, RenameParams,
ServerCapabilities, ServerInfo, TextDocumentPositionParams, TextDocumentSyncCapability,
TextDocumentSyncKind, WorkspaceEdit,
};
use async_lsp::{LanguageClient, LanguageServer, ResponseError};
use futures::future::BoxFuture;
Expand Down Expand Up @@ -42,6 +43,7 @@ impl LanguageServer for ServerState {
hover_provider: Some(HoverProviderCapability::Simple(true)),
document_symbol_provider: Some(OneOf::Left(true)),
completion_provider: Some(CompletionOptions::default()),
rename_provider: Some(OneOf::Left(true)),
..ServerCapabilities::default()
},
server_info: Some(ServerInfo {
Expand Down Expand Up @@ -102,6 +104,46 @@ impl LanguageServer for ServerState {
Box::pin(async move { Ok(Some(CompletionResponse::Array(keywords))) })
}

fn prepare_rename(
&mut self,
params: TextDocumentPositionParams,
) -> BoxFuture<'static, Result<Option<PrepareRenameResponse>, Self::Error>> {
let uri = params.text_document.uri;
let pos = params.position;

match self.get_parsed_tree_and_content(&uri) {
Err(e) => Box::pin(async move { Err(e) }),
Ok((tree, _)) => {
let response = tree.can_rename(&pos).map(PrepareRenameResponse::Range);

Box::pin(async move { Ok(response) })
}
}
}

fn rename(
&mut self,
params: RenameParams,
) -> BoxFuture<'static, Result<Option<WorkspaceEdit>, Self::Error>> {
let uri = params.text_document_position.text_document.uri;
let pos = params.text_document_position.position;

let new_name = params.new_name;

match self.get_parsed_tree_and_content(&uri) {
Err(e) => Box::pin(async move { Err(e) }),
Ok((tree, content)) => {
let response = if tree.can_rename(&pos).is_some() {
tree.rename(&uri, &pos, &new_name, content)
} else {
None
};

Box::pin(async move { Ok(response) })
}
}
}

fn definition(
&mut self,
param: GotoDefinitionParams,
Expand Down
Loading

0 comments on commit fec7bfb

Please sign in to comment.