Skip to content

Commit

Permalink
Detect position of code action range
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Apr 23, 2024
1 parent 393aec3 commit 28e72df
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/code_info/ast_signature.rs
Expand Up @@ -13,6 +13,8 @@ pub struct DefSignatureNode {
pub end_offset: u32,
pub start_line: u32,
pub end_line: u32,
pub start_colum: u16,
pub end_column: u16,

pub children: Vec<DefSignatureNode>,

Expand Down
12 changes: 12 additions & 0 deletions src/code_info_builder/classlike_scanner.rs
Expand Up @@ -502,6 +502,8 @@ pub(crate) fn scan(
end_offset: storage.def_location.end_offset,
start_line: storage.meta_start.start_line,
end_line: storage.def_location.end_line,
start_colum: storage.def_location.start_column,
end_column: storage.def_location.end_column,
children: Vec::new(),
signature_hash,
body_hash: None,
Expand Down Expand Up @@ -673,6 +675,8 @@ pub(crate) fn scan(
end_offset: functionlike_storage.def_location.end_offset,
start_line: functionlike_storage.def_location.start_line,
end_line: functionlike_storage.def_location.end_line,
start_colum: functionlike_storage.def_location.start_column,
end_column: functionlike_storage.def_location.end_column,
signature_hash,
body_hash: Some(body_hash),
children: vec![],
Expand Down Expand Up @@ -842,6 +846,8 @@ fn visit_xhp_attribute(
end_offset: stmt_pos.end_offset,
start_line: stmt_pos.start_line,
end_line: stmt_pos.end_line,
start_colum: stmt_pos.start_column,
end_column: stmt_pos.end_column,
signature_hash: xxhash_rust::xxh3::xxh3_64(
file_source.file_contents[stmt_pos.start_offset as usize..stmt_pos.end_offset as usize]
.as_bytes(),
Expand Down Expand Up @@ -924,6 +930,8 @@ fn visit_class_const_declaration(
end_offset: def_pos.end_offset,
start_line: def_pos.start_line,
end_line: def_pos.end_line,
start_colum: def_pos.start_column,
end_column: def_pos.end_column,
signature_hash: position_insensitive_hash(const_node).wrapping_add(uses_hash),
body_hash: None,
children: vec![],
Expand Down Expand Up @@ -1012,6 +1020,8 @@ fn visit_class_typeconst_declaration(
end_offset: def_pos.end_offset,
start_line: def_pos.start_line,
end_line: def_pos.end_line,
start_colum: def_pos.start_column,
end_column: def_pos.end_column,
signature_hash: position_insensitive_hash(const_node).wrapping_add(uses_hash),
body_hash: None,
children: vec![],
Expand Down Expand Up @@ -1070,6 +1080,8 @@ fn visit_property_declaration(
end_offset: def_pos.end_offset,
start_line: def_pos.start_line,
end_line: def_pos.end_line,
start_colum: def_pos.start_column,
end_column: def_pos.end_column,
signature_hash: xxhash_rust::xxh3::xxh3_64(
file_source.file_contents[def_pos.start_offset as usize..def_pos.end_offset as usize]
.as_bytes(),
Expand Down
6 changes: 6 additions & 0 deletions src/code_info_builder/lib.rs
Expand Up @@ -148,6 +148,8 @@ impl<'ast> Visitor<'ast> for Scanner<'_> {
end_offset: definition_location.end_offset,
start_line: definition_location.start_line,
end_line: definition_location.end_line,
start_colum: definition_location.start_column,
end_column: definition_location.end_column,
children: Vec::new(),
signature_hash: { position_insensitive_hash(gc).wrapping_add(uses_hash) },
body_hash: None,
Expand Down Expand Up @@ -280,6 +282,8 @@ impl<'ast> Visitor<'ast> for Scanner<'_> {
end_offset: definition_location.end_offset,
start_line: definition_location.start_line,
end_line: definition_location.end_line,
start_colum: definition_location.start_column,
end_column: definition_location.end_column,
children: Vec::new(),
signature_hash: { position_insensitive_hash(typedef).wrapping_add(uses_hash) },
body_hash: None,
Expand Down Expand Up @@ -518,6 +522,8 @@ impl<'ast> Visitor<'ast> for Scanner<'_> {
end_offset: functionlike_storage.def_location.end_offset,
start_line: functionlike_storage.def_location.start_line,
end_line: functionlike_storage.def_location.end_line,
start_colum: functionlike_storage.def_location.start_column,
end_column: functionlike_storage.def_location.end_column,
children: Vec::new(),
signature_hash,
body_hash: Some(body_hash),
Expand Down
1 change: 1 addition & 0 deletions src/language_server/Cargo.toml
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
hakana-workhorse = { path = "../file_scanner_analyzer" }
hakana-analyzer = { path = "../analyzer" }
hakana-logger = { path = "../logger" }
hakana-str = { path = "../str" }
hakana-reflection-info = { path = "../code_info" }
tower-lsp = { git = "https://github.com/ebkalderon/tower-lsp", branch = "support-mutable-methods" }
tokio = { version = "1.26.0", features = ["full"] }
Expand Down
59 changes: 54 additions & 5 deletions src/language_server/lib.rs
Expand Up @@ -6,6 +6,8 @@ use hakana_analyzer::config::{self, Config};
use hakana_analyzer::custom_hook::CustomHook;
use hakana_logger::{Logger, Verbosity};
use hakana_reflection_info::analysis_result::AnalysisResult;
use hakana_reflection_info::code_location::FilePath;
use hakana_str::StrId;
use hakana_workhorse::file::FileStatus;
use hakana_workhorse::{scan_and_analyze_async, SuccessfulScanData};
use rustc_hash::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -54,6 +56,12 @@ impl LanguageServer for Backend {
save: Some(TextDocumentSyncSaveOptions::Supported(true)),
},
)),
code_action_provider: Some(CodeActionProviderCapability::Options(
CodeActionOptions {
code_action_kinds: Some(vec![CodeActionKind::REFACTOR_REWRITE]),
..CodeActionOptions::default()
},
)),
..ServerCapabilities::default()
},
..InitializeResult::default()
Expand Down Expand Up @@ -141,11 +149,11 @@ impl LanguageServer for Backend {
}

self.client
.log_message(
MessageType::INFO,
format!("receiving changes {:?}", new_file_statuses),
)
.await;
.log_message(
MessageType::INFO,
format!("receiving changes {:?}", new_file_statuses),
)
.await;

if let Some(ref mut existing_file_changes) = self.file_changes {
existing_file_changes.extend(new_file_statuses);
Expand All @@ -170,6 +178,47 @@ impl LanguageServer for Backend {
}
}

async fn code_action(&self, params: CodeActionParams) -> Result<Option<CodeActionResponse>> {
if let Some(previous_scan_data) = &*self.previous_scan_data {
if let Some(strid) = previous_scan_data
.interner
.get(params.text_document.uri.path())
{
let file_path = FilePath(strid);
if let (Some(_), Some(file_info)) = (
previous_scan_data
.file_system
.file_hashes_and_times
.get(&file_path),
previous_scan_data.codebase.files.get(&file_path),
) {
let mut node_ref = (StrId::EMPTY, StrId::EMPTY);

for ast_node in &file_info.ast_nodes {
if (params.range.start.line + 1 >= ast_node.start_line)
&& (params.range.end.line + 1 < ast_node.end_line)
{
node_ref.0 = ast_node.name;

for child_node in &ast_node.children {
if (params.range.start.line + 1 >= child_node.start_line)
&& (params.range.end.line + 1 < child_node.end_line)
{
node_ref.1 = child_node.name;
break;
}
}

break;
}
}
}
}
}

Err(tower_lsp::jsonrpc::Error::method_not_found())
}

async fn shutdown(&mut self) -> Result<()> {
Ok(())
}
Expand Down

0 comments on commit 28e72df

Please sign in to comment.