aboutsummaryrefslogtreecommitdiff
path: root/src/server/action.odin
diff options
context:
space:
mode:
authorDanielGavin <danielgavin5@hotmail.com>2025-09-22 22:04:24 +0200
committerDanielGavin <danielgavin5@hotmail.com>2025-09-22 22:04:24 +0200
commit28d540bb5f838815e23622d97cd5d3a55776414d (patch)
tree6fbe43699e08753379a2ab00982f0946321ec798 /src/server/action.odin
parent1009de179a717c8b355acb8b1268fedc9b2d089c (diff)
Add new code action: remove unused imports
Diffstat (limited to 'src/server/action.odin')
-rw-r--r--src/server/action.odin42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/server/action.odin b/src/server/action.odin
index 0c3c74a..0c522bf 100644
--- a/src/server/action.odin
+++ b/src/server/action.odin
@@ -67,11 +67,53 @@ get_code_actions :: proc(document: ^Document, range: common.Range, config: ^comm
if selector, ok := position_context.selector_expr.derived.(^ast.Selector_Expr); ok {
add_missing_imports(&ast_context, selector, strings.clone(document.uri.uri), config, &actions)
}
+ } else if position_context.import_stmt != nil {
+ remove_missing_imports(document, strings.clone(document.uri.uri), config, &actions)
}
return actions[:], true
}
+remove_missing_imports :: proc(
+ document: ^Document,
+ uri: string,
+ config: ^common.Config,
+ actions: ^[dynamic]CodeAction,
+) {
+ unused_imports := find_unused_imports(document, context.temp_allocator)
+
+ if len(unused_imports) == 0 {
+ return
+ }
+
+ textEdits := make([dynamic]TextEdit, context.temp_allocator)
+
+ for imp in unused_imports {
+ range := common.get_token_range(imp.import_decl, document.ast.src)
+ import_edit := TextEdit {
+ range = range,
+ newText = "",
+ }
+
+ append(&textEdits, import_edit)
+ }
+
+ workspaceEdit: WorkspaceEdit
+ workspaceEdit.changes = make(map[string][]TextEdit, 0, context.temp_allocator)
+ workspaceEdit.changes[uri] = textEdits[:]
+
+ append(
+ actions,
+ CodeAction {
+ kind = "refactor.rewrite",
+ isPreferred = true,
+ title = fmt.tprint("remove unused imports"),
+ edit = workspaceEdit,
+ },
+ )
+
+}
+
add_missing_imports :: proc(
ast_context: ^AstContext,
selector: ^ast.Selector_Expr,