mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-28 04:09:05 +00:00
feat(els): implement extract into function
This commit is contained in:
parent
b8b312caad
commit
bb1dd5eb8e
4 changed files with 83 additions and 15 deletions
|
@ -1,5 +1,7 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use erg_common::consts::ERG_MODE;
|
||||
use erg_common::deepen_indent;
|
||||
use erg_common::traits::Locational;
|
||||
use erg_compiler::artifact::BuildRunnable;
|
||||
use erg_compiler::erg_parser::token::{Token, TokenKind};
|
||||
|
@ -7,7 +9,7 @@ use erg_compiler::hir::Expr;
|
|||
|
||||
use lsp_types::{
|
||||
CodeAction, CodeActionKind, CodeActionOrCommand, CodeActionParams, CodeActionResponse,
|
||||
TextEdit, Url, WorkspaceEdit,
|
||||
Position, Range, TextEdit, Url, WorkspaceEdit,
|
||||
};
|
||||
|
||||
use crate::server::{send_log, ELSResult, Server};
|
||||
|
@ -16,10 +18,10 @@ use crate::util::{self, NormalizedUrl};
|
|||
impl<Checker: BuildRunnable> Server<Checker> {
|
||||
fn gen_eliminate_unused_vars_action(
|
||||
&self,
|
||||
params: CodeActionParams,
|
||||
params: &CodeActionParams,
|
||||
) -> ELSResult<Option<CodeAction>> {
|
||||
let uri = NormalizedUrl::new(params.text_document.uri);
|
||||
let diags = params.context.diagnostics;
|
||||
let uri = NormalizedUrl::new(params.text_document.uri.clone());
|
||||
let diags = ¶ms.context.diagnostics;
|
||||
let Some(diag) = diags.get(0).cloned() else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
@ -134,7 +136,17 @@ impl<Checker: BuildRunnable> Server<Checker> {
|
|||
Some(action)
|
||||
}
|
||||
|
||||
fn send_normal_action(&self, params: CodeActionParams) -> ELSResult<Vec<CodeAction>> {
|
||||
fn gen_extract_function_action(&self, params: &CodeActionParams) -> Option<CodeAction> {
|
||||
let action = CodeAction {
|
||||
title: "Extract into function".to_string(),
|
||||
kind: Some(CodeActionKind::REFACTOR_EXTRACT),
|
||||
data: Some(serde_json::to_value(params.clone()).unwrap()),
|
||||
..Default::default()
|
||||
};
|
||||
Some(action)
|
||||
}
|
||||
|
||||
fn send_normal_action(&self, params: &CodeActionParams) -> ELSResult<Vec<CodeAction>> {
|
||||
let mut actions = vec![];
|
||||
let uri = NormalizedUrl::new(params.text_document.uri.clone());
|
||||
if let Some(token) = self.file_cache.get_token(&uri, params.range.start) {
|
||||
|
@ -144,10 +156,11 @@ impl<Checker: BuildRunnable> Server<Checker> {
|
|||
}
|
||||
}
|
||||
actions.extend(self.send_quick_fix(params)?);
|
||||
actions.extend(self.gen_extract_function_action(params));
|
||||
Ok(actions)
|
||||
}
|
||||
|
||||
fn send_quick_fix(&self, params: CodeActionParams) -> ELSResult<Vec<CodeAction>> {
|
||||
fn send_quick_fix(&self, params: &CodeActionParams) -> ELSResult<Vec<CodeAction>> {
|
||||
let mut result: Vec<CodeAction> = vec![];
|
||||
let diags = ¶ms.context.diagnostics;
|
||||
if diags.is_empty() {
|
||||
|
@ -174,8 +187,8 @@ impl<Checker: BuildRunnable> Server<Checker> {
|
|||
.as_ref()
|
||||
.and_then(|kinds| kinds.first().map(|s| s.as_str()))
|
||||
{
|
||||
Some("quickfix") => self.send_quick_fix(params)?,
|
||||
None => self.send_normal_action(params)?,
|
||||
Some("quickfix") => self.send_quick_fix(¶ms)?,
|
||||
None => self.send_normal_action(¶ms)?,
|
||||
Some(other) => {
|
||||
send_log(&format!("Unknown code action requested: {other}"))?;
|
||||
vec![]
|
||||
|
@ -188,4 +201,42 @@ impl<Checker: BuildRunnable> Server<Checker> {
|
|||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn handle_code_action_resolve(
|
||||
&mut self,
|
||||
action: CodeAction,
|
||||
) -> ELSResult<CodeAction> {
|
||||
send_log(format!("code action resolve requested: {action:?}"))?;
|
||||
match &action.title[..] {
|
||||
"Extract into function" => self.resolve_extract_function_action(action),
|
||||
_ => Ok(action),
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_extract_function_action(&self, mut action: CodeAction) -> ELSResult<CodeAction> {
|
||||
let params = action
|
||||
.data
|
||||
.take()
|
||||
.and_then(|v| serde_json::from_value::<CodeActionParams>(v).ok())
|
||||
.ok_or("invalid params")?;
|
||||
let uri = NormalizedUrl::new(params.text_document.uri);
|
||||
let range = Range::new(Position::new(params.range.start.line, 0), params.range.end);
|
||||
let code = self.file_cache.get_ranged(&uri, range)?.unwrap_or_default();
|
||||
let indent_len = code.chars().take_while(|c| *c == ' ').count();
|
||||
let code = deepen_indent(code);
|
||||
let new_func = if ERG_MODE {
|
||||
format!("{}new_func() =\n{code}\n\n", " ".repeat(indent_len))
|
||||
} else {
|
||||
format!("{}def new_func():\n{code}\n\n", " ".repeat(indent_len))
|
||||
};
|
||||
let edit1 = TextEdit::new(range, new_func);
|
||||
let edit2 = TextEdit::new(
|
||||
Range::new(range.end, range.end),
|
||||
format!("{}new_func()", " ".repeat(indent_len)),
|
||||
);
|
||||
let mut changes = HashMap::new();
|
||||
changes.insert(uri.raw(), vec![edit1, edit2]);
|
||||
action.edit = Some(WorkspaceEdit::new(changes));
|
||||
Ok(action)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue