aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanielGavin <danielgavin5@hotmail.com>2025-09-27 22:08:25 +0200
committerDanielGavin <danielgavin5@hotmail.com>2025-09-27 22:08:25 +0200
commitfadba86f623fd0c0e0399b88b79804c5e466ef95 (patch)
treedc33bec9b25c5f93f79ba8a14b78e8abfd63cdd3 /src
parent2be257718c637e2f42b760fe9acca3eb7674530a (diff)
Have the removed imports remove the line.
Diffstat (limited to 'src')
-rw-r--r--src/common/position.odin14
-rw-r--r--src/server/action.odin18
-rw-r--r--src/server/imports.odin6
3 files changed, 32 insertions, 6 deletions
diff --git a/src/common/position.odin b/src/common/position.odin
index e5dc2bd..4d01f58 100644
--- a/src/common/position.odin
+++ b/src/common/position.odin
@@ -133,6 +133,20 @@ get_token_range :: proc(node: ast.Node, document_text: string) -> (range: Range)
return
}
+get_last_column :: proc(line: int, document_text: []u8) -> (int, bool) {
+ line_count := 0
+ index := 1
+ last := document_text[0]
+
+ if !get_index_at_line(&index, &line_count, &last, document_text[:], line) {
+ return {}, false
+ }
+
+ column := get_character_offset_u8_to_u16(100000, document_text[index:])
+
+ return column, true
+}
+
get_absolute_range :: proc(range: Range, document_text: []u8) -> (AbsoluteRange, bool) {
absolute: AbsoluteRange
diff --git a/src/server/action.odin b/src/server/action.odin
index 0c522bf..058ea21 100644
--- a/src/server/action.odin
+++ b/src/server/action.odin
@@ -68,13 +68,13 @@ get_code_actions :: proc(document: ^Document, range: common.Range, config: ^comm
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)
+ remove_unused_imports(document, strings.clone(document.uri.uri), config, &actions)
}
return actions[:], true
}
-remove_missing_imports :: proc(
+remove_unused_imports :: proc(
document: ^Document,
uri: string,
config: ^common.Config,
@@ -90,14 +90,26 @@ remove_missing_imports :: proc(
for imp in unused_imports {
range := common.get_token_range(imp.import_decl, document.ast.src)
+
import_edit := TextEdit {
- range = range,
+ range = range,
newText = "",
}
+ if (range.start.line != 1) {
+ if column, ok := common.get_last_column(import_edit.range.start.line - 1, document.text); ok {
+ import_edit.range.start.line -= 1
+ import_edit.range.start.character = column
+ }
+
+ }
+
+
append(&textEdits, import_edit)
}
+ log.error(textEdits[:])
+
workspaceEdit: WorkspaceEdit
workspaceEdit.changes = make(map[string][]TextEdit, 0, context.temp_allocator)
workspaceEdit.changes[uri] = textEdits[:]
diff --git a/src/server/imports.odin b/src/server/imports.odin
index 4da5a86..498af33 100644
--- a/src/server/imports.odin
+++ b/src/server/imports.odin
@@ -15,12 +15,12 @@ find_unused_imports :: proc(document: ^Document, allocator := context.temp_alloc
context.allocator = runtime.arena_allocator(&arena)
- symbols_and_nodes := resolve_entire_file_cached(document)
+ symbols_and_nodes := resolve_entire_file(document)
- pkgs := make(map[string]bool, context.temp_allocator)
+ pkgs := make(map[string]struct{}, context.temp_allocator)
for _, v in symbols_and_nodes {
- pkgs[v.symbol.pkg] = true
+ pkgs[v.symbol.pkg] = {}
}
unused := make([dynamic]Package, allocator)