Don't categorize things we don't care about

This commit is contained in:
kjeremy 2020-07-02 18:01:22 -04:00
parent 36cc81ac71
commit 4c9347ecc3
6 changed files with 51 additions and 67 deletions

View file

@ -29,7 +29,7 @@ pub(crate) fn add_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?; let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
let node_start = derive_insertion_offset(&nominal)?; let node_start = derive_insertion_offset(&nominal)?;
let target = nominal.syntax().text_range(); let target = nominal.syntax().text_range();
acc.add(AssistId("add_derive", AssistKind::Refactor), "Add `#[derive]`", target, |builder| { acc.add(AssistId("add_derive", AssistKind::None), "Add `#[derive]`", target, |builder| {
let derive_attr = nominal let derive_attr = nominal
.attrs() .attrs()
.filter_map(|x| x.as_simple_call()) .filter_map(|x| x.as_simple_call())

View file

@ -62,20 +62,15 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?; let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
let target = call.syntax().text_range(); let target = call.syntax().text_range();
acc.add( acc.add(AssistId("add_function", AssistKind::None), "Add function", target, |builder| {
AssistId("add_function", AssistKind::RefactorExtract), let function_template = function_builder.render();
"Add function", builder.edit_file(function_template.file);
target, let new_fn = function_template.to_string(ctx.config.snippet_cap);
|builder| { match ctx.config.snippet_cap {
let function_template = function_builder.render(); Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
builder.edit_file(function_template.file); None => builder.insert(function_template.insert_offset, new_fn),
let new_fn = function_template.to_string(ctx.config.snippet_cap); }
match ctx.config.snippet_cap { })
Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
None => builder.insert(function_template.insert_offset, new_fn),
}
},
)
} }
struct FunctionTemplate { struct FunctionTemplate {

View file

@ -42,55 +42,50 @@ pub(crate) fn add_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let impl_def = find_struct_impl(&ctx, &strukt)?; let impl_def = find_struct_impl(&ctx, &strukt)?;
let target = strukt.syntax().text_range(); let target = strukt.syntax().text_range();
acc.add( acc.add(AssistId("add_new", AssistKind::None), "Add default constructor", target, |builder| {
AssistId("add_new", AssistKind::Refactor), let mut buf = String::with_capacity(512);
"Add default constructor",
target,
|builder| {
let mut buf = String::with_capacity(512);
if impl_def.is_some() { if impl_def.is_some() {
buf.push('\n');
}
let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
let params = field_list
.fields()
.filter_map(|f| {
Some(format!("{}: {}", f.name()?.syntax(), f.ascribed_type()?.syntax()))
})
.sep_by(", ");
let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", ");
format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields);
let start_offset = impl_def
.and_then(|impl_def| {
buf.push('\n'); buf.push('\n');
let start = impl_def
.syntax()
.descendants_with_tokens()
.find(|t| t.kind() == T!['{'])?
.text_range()
.end();
Some(start)
})
.unwrap_or_else(|| {
buf = generate_impl_text(&strukt, &buf);
strukt.syntax().text_range().end()
});
match ctx.config.snippet_cap {
None => builder.insert(start_offset, buf),
Some(cap) => {
buf = buf.replace("fn new", "fn $0new");
builder.insert_snippet(cap, start_offset, buf);
} }
}
let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v)); })
let params = field_list
.fields()
.filter_map(|f| {
Some(format!("{}: {}", f.name()?.syntax(), f.ascribed_type()?.syntax()))
})
.sep_by(", ");
let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", ");
format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields);
let start_offset = impl_def
.and_then(|impl_def| {
buf.push('\n');
let start = impl_def
.syntax()
.descendants_with_tokens()
.find(|t| t.kind() == T!['{'])?
.text_range()
.end();
Some(start)
})
.unwrap_or_else(|| {
buf = generate_impl_text(&strukt, &buf);
strukt.syntax().text_range().end()
});
match ctx.config.snippet_cap {
None => builder.insert(start_offset, buf),
Some(cap) => {
buf = buf.replace("fn new", "fn $0new");
builder.insert_snippet(cap, start_offset, buf);
}
}
},
)
} }
// Generates the surrounding `impl Type { <code> }` including type and lifetime // Generates the surrounding `impl Type { <code> }` including type and lifetime

View file

@ -34,8 +34,6 @@ pub enum AssistKind {
RefactorExtract, RefactorExtract,
RefactorInline, RefactorInline,
RefactorRewrite, RefactorRewrite,
Source,
OrganizeImports,
} }
/// Unique identifier of the assist, should not be shown to the user /// Unique identifier of the assist, should not be shown to the user

View file

@ -112,8 +112,6 @@ fn code_action_capabilities(client_caps: &ClientCapabilities) -> CodeActionProvi
lsp_types::code_action_kind::REFACTOR_EXTRACT.to_string(), lsp_types::code_action_kind::REFACTOR_EXTRACT.to_string(),
lsp_types::code_action_kind::REFACTOR_INLINE.to_string(), lsp_types::code_action_kind::REFACTOR_INLINE.to_string(),
lsp_types::code_action_kind::REFACTOR_REWRITE.to_string(), lsp_types::code_action_kind::REFACTOR_REWRITE.to_string(),
lsp_types::code_action_kind::SOURCE.to_string(),
lsp_types::code_action_kind::SOURCE_ORGANIZE_IMPORTS.to_string(),
]), ]),
work_done_progress_options: Default::default(), work_done_progress_options: Default::default(),
}) })

View file

@ -635,8 +635,6 @@ pub(crate) fn code_action_kind(kind: AssistKind) -> String {
AssistKind::RefactorExtract => lsp_types::code_action_kind::REFACTOR_EXTRACT, AssistKind::RefactorExtract => lsp_types::code_action_kind::REFACTOR_EXTRACT,
AssistKind::RefactorInline => lsp_types::code_action_kind::REFACTOR_INLINE, AssistKind::RefactorInline => lsp_types::code_action_kind::REFACTOR_INLINE,
AssistKind::RefactorRewrite => lsp_types::code_action_kind::REFACTOR_REWRITE, AssistKind::RefactorRewrite => lsp_types::code_action_kind::REFACTOR_REWRITE,
AssistKind::Source => lsp_types::code_action_kind::SOURCE,
AssistKind::OrganizeImports => lsp_types::code_action_kind::SOURCE_ORGANIZE_IMPORTS,
} }
.to_string() .to_string()
} }