From fadba86f623fd0c0e0399b88b79804c5e466ef95 Mon Sep 17 00:00:00 2001 From: DanielGavin Date: Sat, 27 Sep 2025 22:08:25 +0200 Subject: Have the removed imports remove the line. --- src/common/position.odin | 14 ++++++++++++++ src/server/action.odin | 18 +++++++++++++++--- src/server/imports.odin | 6 +++--- 3 files changed, 32 insertions(+), 6 deletions(-) (limited to 'src') 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) -- cgit v1.2.3