Skip to content

Commit

Permalink
Merge pull request #27 from Jeansidharta/feat/prepare-rename-request
Browse files Browse the repository at this point in the history
Feat: prepare rename request
  • Loading branch information
Leathong authored Jan 2, 2024
2 parents bee3166 + 47f6a0b commit a968de2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/server/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use lsp_types::{
DidSaveTextDocument,
},
request::{
Completion, DocumentSymbolRequest, Formatting, GotoDefinition, HoverRequest, Rename,
Completion, DocumentSymbolRequest, Formatting, GotoDefinition, HoverRequest,
PrepareRenameRequest, Rename,
},
};
use serde_json::json;
Expand Down Expand Up @@ -71,6 +72,7 @@ impl Server {
let req = proc_req!(req, GotoDefinition, handle_definition);
let req = proc_req!(req, DocumentSymbolRequest, handle_document_symbols);
let req = proc_req!(req, Formatting, handle_formatting);
let req = proc_req!(req, PrepareRenameRequest, handle_prepare_rename);
let req = proc_req!(req, Rename, handle_rename);
err_to_console!("unknown request: {:?}", req);
}
Expand Down
78 changes: 77 additions & 1 deletion src/server/handler/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use lsp_types::{
DocumentFormattingParams, DocumentSymbolParams, DocumentSymbolResponse, Documentation,
GotoDefinitionParams, GotoDefinitionResponse, Hover, HoverContents, HoverParams,
InsertTextFormat, InsertTextMode, Location, MarkupContent, Range, RenameParams,
SymbolInformation, TextEdit, WorkspaceEdit,
SymbolInformation, TextDocumentPositionParams, TextEdit, WorkspaceEdit,
};

use tree_sitter::{Node, Point};
Expand All @@ -32,6 +32,82 @@ fn get_node_at_point<'a>(parsed_code: &'a Ref<'_, ParsedCode>, point: Point) ->

// Request handlers.
impl Server {
pub(crate) fn handle_prepare_rename(
&mut self,
id: RequestId,
params: TextDocumentPositionParams,
) {
let uri = params.text_document.uri;

let file = match self.get_code(&uri) {
Some(code) => code,
_ => return,
};
file.borrow_mut().gen_top_level_items_if_needed();
let bfile = file.borrow();

let node = get_node_at_point(&bfile, to_point(params.position));
if node.kind() != "identifier" {
self.respond(Response {
id,
result: None,
error: None,
});
return;
}
let ident_name = node_text(&bfile.code, &node);
let identifier_definition =
self.find_identities(&file.borrow(), &|name| name == ident_name, &node, false, 0);

let definition = if let Some(def) = identifier_definition.get(0) {
def
} else {
self.respond(Response {
id,
result: None,
error: None,
});
return;
};

let url = if let Some(url) = definition.borrow().url.clone() {
url
} else {
self.respond(Response {
id,
result: None,
error: None,
});
return;
};

if url != uri {
self.respond(Response {
id,
result: None,
error: Some(ResponseError {
code: 0,
message:
"Sorry, but renaming symbols defined in another file is not yet supported"
.to_string(),
data: None,
}),
});
return;
}

self.respond(Response {
id,
result: Some(
serde_json::to_value(Range {
start: to_position(node.start_position()),
end: to_position(node.end_position()),
})
.unwrap(),
),
error: None,
})
}
pub(crate) fn handle_rename(&mut self, id: RequestId, params: RenameParams) {
let uri = params.text_document_position.text_document.uri;
let ident_new_name = params.new_name;
Expand Down
4 changes: 2 additions & 2 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Server {
}

pub(crate) fn main_loop(&mut self) -> Result<(), Box<dyn Error + Sync + Send>> {
let caps = serde_json::to_value(&ServerCapabilities {
let caps = serde_json::to_value(ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::INCREMENTAL,
)),
Expand All @@ -200,7 +200,7 @@ impl Server {
document_symbol_provider: Some(OneOf::Left(true)),
document_formatting_provider: Some(OneOf::Left(true)),
rename_provider: Some(OneOf::Right(RenameOptions {
prepare_provider: None,
prepare_provider: Some(true),
work_done_progress_options: WorkDoneProgressOptions::default(),
})),
..Default::default()
Expand Down

0 comments on commit a968de2

Please # to comment.