aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanielGavin <danielgavin5@hotmail.com>2020-12-08 19:14:17 +0100
committerDanielGavin <danielgavin5@hotmail.com>2020-12-08 19:14:17 +0100
commit6b122c25ebb9d60d2b528f2f2f5d84218a3dad6d (patch)
tree30611979cf569e7357669270d6aa4703b7224882
parent9585a8c0d8f85f78696219b74e8353bbdaf3db4f (diff)
more refractor on managing package changing on symbols
-rw-r--r--src/index/clone.odin18
-rw-r--r--src/index/collector.odin2
-rw-r--r--src/index/indexer.odin24
-rw-r--r--src/index/memory_index.odin9
-rw-r--r--src/server/analysis.odin52
5 files changed, 79 insertions, 26 deletions
diff --git a/src/index/clone.odin b/src/index/clone.odin
index 19a4526..aa07f3b 100644
--- a/src/index/clone.odin
+++ b/src/index/clone.odin
@@ -74,8 +74,22 @@ clone_node :: proc(node: ^ast.Node, allocator: mem.Allocator, unique_strings: ^m
mem.copy(res, src, size);
res.derived.data = rawptr(res);
- res.pos.file = "";
- res.end.file = "";
+ if unique_strings != nil && node.pos.file != "" {
+ res.pos.file = get_index_unique_string(unique_strings, allocator, node.pos.file);
+ }
+
+ else {
+ res.pos.file = node.pos.file;
+ }
+
+ if unique_strings != nil && node.end.file != "" {
+ res.end.file = get_index_unique_string(unique_strings, allocator, node.end.file);
+ }
+
+ else {
+ res.end.file = node.end.file;
+ }
+
switch n in node.derived {
case Bad_Expr:
diff --git a/src/index/collector.odin b/src/index/collector.odin
index e43b563..070b5ad 100644
--- a/src/index/collector.odin
+++ b/src/index/collector.odin
@@ -300,7 +300,7 @@ get_package_mapping :: proc(file: ast.File, config: ^common.Config, uri: string)
name: string;
- base := path.base(uri);
+ base := path.base(uri, false, context.temp_allocator);
full := path.join(elems = {base, imp.fullpath[1:len(imp.fullpath)-1]}, allocator = context.temp_allocator);
diff --git a/src/index/indexer.odin b/src/index/indexer.odin
index 0cd25fe..9c7b902 100644
--- a/src/index/indexer.odin
+++ b/src/index/indexer.odin
@@ -43,15 +43,31 @@ Indexer :: struct {
indexer: Indexer;
+FuzzyResult :: struct {
+ symbol: Symbol,
+ weight: f32,
+};
+
lookup :: proc(name: string, pkg: string, loc := #caller_location) -> (Symbol, bool) {
- symbol, ok := memory_index_lookup(&indexer.static_index, name, pkg);
- log.infof("lookup name: %v pkg: %v, symbol %v location %v", name, pkg, symbol, loc);
- return symbol, ok;
+
+ if symbol, ok := memory_index_lookup(&indexer.static_index, name, pkg); ok {
+ log.infof("lookup name: %v pkg: %v, symbol %v location %v", name, pkg, symbol, loc);
+ return symbol, true;
+ }
+
+ if symbol, ok := memory_index_lookup(&indexer.static_index, name, "builtin"); ok {
+ log.infof("lookup name: %v pkg: %v, symbol %v location %v", name, pkg, symbol, loc);
+ return symbol, true;
+ }
+
+
+ log.infof("lookup failed name: %v pkg: %v location %v", name, pkg, loc);
+ return {}, false;
}
-fuzzy_search :: proc(name: string, pkgs: [] string) -> ([] Symbol, bool) {
+fuzzy_search :: proc(name: string, pkgs: [] string) -> ([] FuzzyResult, bool) {
return memory_index_fuzzy_search(&indexer.static_index, name, pkgs);
}
diff --git a/src/index/memory_index.odin b/src/index/memory_index.odin
index c7ab26e..4948b4d 100644
--- a/src/index/memory_index.odin
+++ b/src/index/memory_index.odin
@@ -31,9 +31,9 @@ memory_index_lookup :: proc(index: ^MemoryIndex, name: string, pkg: string) -> (
return index.collection.symbols[id];
}
-memory_index_fuzzy_search :: proc(index: ^MemoryIndex, name: string, pkgs: [] string) -> ([] Symbol, bool) {
+memory_index_fuzzy_search :: proc(index: ^MemoryIndex, name: string, pkgs: [] string) -> ([] FuzzyResult, bool) {
- symbols := make([dynamic] Symbol, 0, context.temp_allocator);
+ symbols := make([dynamic] FuzzyResult, 0, context.temp_allocator);
fuzzy_matcher := common.make_fuzzy_matcher(name);
@@ -51,8 +51,9 @@ memory_index_fuzzy_search :: proc(index: ^MemoryIndex, name: string, pkgs: [] st
}
if name == "" || common.fuzzy_match(fuzzy_matcher, symbol.name) > 0.5 {
- append(&symbols, symbol);
- i += 1;
+ result := FuzzyResult {symbol = symbol};
+ append(&symbols, result);
+ i += 1;
}
diff --git a/src/server/analysis.odin b/src/server/analysis.odin
index b786597..c336566 100644
--- a/src/server/analysis.odin
+++ b/src/server/analysis.odin
@@ -9,6 +9,7 @@ import "core:strings"
import "core:path"
import "core:mem"
import "core:strconv"
+import "core:path/filepath"
import "shared:common"
import "shared:index"
@@ -42,6 +43,7 @@ AstContext :: struct {
globals: map [string] ^ast.Expr,
variables: map [string] bool,
parameters: map [string] bool,
+ in_package: map[string] string, //sometimes you have to extract types from arrays/maps and you lose package information
usings: [dynamic] string,
file: ast.File,
allocator: mem.Allocator,
@@ -61,6 +63,7 @@ make_ast_context :: proc(file: ast.File, imports: [] Package, package_name: stri
variables = make(map [string] bool, 0, allocator),
usings = make([dynamic] string, allocator),
parameters = make(map [string] bool, 0, allocator),
+ in_package = make(map[string] string, 0, allocator),
file = file,
imports = imports,
use_locals = true,
@@ -568,13 +571,13 @@ resolve_type_expression :: proc(ast_context: ^AstContext, node: ^ast.Expr) -> (i
if len(s.return_types) == 1 {
//ERROR wierd signature help index.new_type() (maybe generics problem)
- selector_expr := index.new_type(ast.Selector_Expr, {}, {}, context.temp_allocator);
+ selector_expr := index.new_type(ast.Selector_Expr, s.return_types[0].node.pos, s.return_types[0].node.end, context.temp_allocator);
selector_expr.expr = s.return_types[0].type;
selector_expr.field = v.field;
return resolve_type_expression(ast_context, selector_expr);
}
case index.SymbolStructValue:
- if selector.uri != "" {
+ if selector.pkg != "" {
ast_context.current_package = selector.pkg;
}
@@ -622,7 +625,7 @@ resolve_type_expression :: proc(ast_context: ^AstContext, node: ^ast.Expr) -> (i
#partial switch s2 in symbol.value {
case index.SymbolStructValue:
- if selector.uri != "" {
+ if selector.pkg != "" {
ast_context.current_package = symbol.pkg;
}
@@ -654,7 +657,6 @@ resolve_type_expression :: proc(ast_context: ^AstContext, node: ^ast.Expr) -> (i
}
return index.Symbol {}, false;
-
}
@@ -666,6 +668,10 @@ resolve_type_identifier :: proc(ast_context: ^AstContext, node: ast.Ident) -> (i
using ast;
+ if pkg, ok := ast_context.in_package[node.name]; ok {
+ ast_context.current_package = pkg;
+ }
+
if _, ok := ast_context.parameters[node.name]; ok {
for imp in ast_context.imports {
@@ -943,12 +949,18 @@ make_int_ast :: proc() -> ^ast.Ident {
return ident;
}
+get_package_from_node :: proc(node: ast.Node) -> string {
+ slashed, _ := filepath.to_slash(node.pos.file, context.temp_allocator);
+ ret := strings.to_lower(path.dir(slashed, context.temp_allocator));
+ return ret;
+}
+
make_symbol_procedure_from_ast :: proc(ast_context: ^AstContext, v: ast.Proc_Lit, name: string) -> index.Symbol {
symbol := index.Symbol {
range = common.get_token_range(v, ast_context.file.src),
type = .Function,
- pkg = ast_context.current_package,
+ pkg = get_package_from_node(v.node),
};
symbol.name = name;
@@ -988,7 +1000,7 @@ make_symbol_generic_from_ast :: proc(ast_context: ^AstContext, expr: ^ast.Expr)
symbol := index.Symbol {
range = common.get_token_range(expr, ast_context.file.src),
type = .Variable,
- pkg = ast_context.current_package,
+ pkg = get_package_from_node(expr^),
signature = common.get_ast_node_string(expr, ast_context.file.src),
};
@@ -1004,7 +1016,7 @@ make_symbol_union_from_ast :: proc(ast_context: ^AstContext, v: ast.Union_Type)
symbol := index.Symbol {
range = common.get_token_range(v, ast_context.file.src),
type = .Enum,
- pkg = ast_context.current_package,
+ pkg = get_package_from_node(v.node),
};
names := make([dynamic] string, context.temp_allocator);
@@ -1029,7 +1041,7 @@ make_symbol_enum_from_ast :: proc(ast_context: ^AstContext, v: ast.Enum_Type) ->
symbol := index.Symbol {
range = common.get_token_range(v, ast_context.file.src),
type = .Enum,
- pkg = ast_context.current_package,
+ pkg = get_package_from_node(v.node),
};
names := make([dynamic] string, context.temp_allocator);
@@ -1054,7 +1066,7 @@ make_symbol_struct_from_ast :: proc(ast_context: ^AstContext, v: ast.Struct_Type
symbol := index.Symbol {
range = common.get_token_range(v, ast_context.file.src),
type = .Struct,
- pkg = ast_context.current_package,
+ pkg = get_package_from_node(v.node),
};
names := make([dynamic] string, context.temp_allocator);
@@ -1234,6 +1246,9 @@ get_locals_stmt :: proc(file: ast.File, stmt: ^ast.Stmt, ast_context: ^AstContex
get_locals_assign_stmt(file, v, ast_context);
case Using_Stmt:
get_locals_using_stmt(file, v, ast_context);
+ case When_Stmt:
+ get_locals_stmt(file, v.else_stmt, ast_context, document_position);
+ get_locals_stmt(file, v.body, ast_context, document_position);
case:
//log.debugf("default node local stmt %v", v);
}
@@ -1311,7 +1326,6 @@ get_locals_if_stmt :: proc(file: ast.File, stmt: ast.If_Stmt, ast_context: ^AstC
get_locals_stmt(file, stmt.else_stmt, ast_context, document_position);
}
-
get_locals_for_range_stmt :: proc(file: ast.File, stmt: ast.Range_Stmt, ast_context: ^AstContext, document_position: ^DocumentPositionContext) {
using ast;
@@ -1338,6 +1352,7 @@ get_locals_for_range_stmt :: proc(file: ast.File, stmt: ast.Range_Stmt, ast_cont
if ident, ok := stmt.val0.derived.(Ident); ok {
ast_context.locals[ident.name] = v.key;
ast_context.variables[ident.name] = true;
+ ast_context.in_package[ident.name] = symbol.pkg;
}
}
@@ -1347,6 +1362,7 @@ get_locals_for_range_stmt :: proc(file: ast.File, stmt: ast.Range_Stmt, ast_cont
if ident, ok := stmt.val1.derived.(Ident); ok {
ast_context.locals[ident.name] = v.value;
ast_context.variables[ident.name] = true;
+ ast_context.in_package[ident.name] = symbol.pkg;
}
}
@@ -1356,6 +1372,7 @@ get_locals_for_range_stmt :: proc(file: ast.File, stmt: ast.Range_Stmt, ast_cont
if ident, ok := stmt.val0.derived.(Ident); ok {
ast_context.locals[ident.name] = v.elem;
ast_context.variables[ident.name] = true;
+ ast_context.in_package[ident.name] = symbol.pkg;
}
}
@@ -1365,6 +1382,7 @@ get_locals_for_range_stmt :: proc(file: ast.File, stmt: ast.Range_Stmt, ast_cont
if ident, ok := stmt.val1.derived.(Ident); ok {
ast_context.locals[ident.name] = make_int_ast();
ast_context.variables[ident.name] = true;
+ ast_context.in_package[ident.name] = symbol.pkg;
}
}
@@ -1374,6 +1392,7 @@ get_locals_for_range_stmt :: proc(file: ast.File, stmt: ast.Range_Stmt, ast_cont
if ident, ok := stmt.val0.derived.(Ident); ok {
ast_context.locals[ident.name] = v.elem;
ast_context.variables[ident.name] = true;
+ ast_context.in_package[ident.name] = symbol.pkg;
}
}
@@ -1383,6 +1402,7 @@ get_locals_for_range_stmt :: proc(file: ast.File, stmt: ast.Range_Stmt, ast_cont
if ident, ok := stmt.val1.derived.(Ident); ok {
ast_context.locals[ident.name] = make_int_ast();
ast_context.variables[ident.name] = true;
+ ast_context.in_package[ident.name] = symbol.pkg;
}
}
@@ -1664,7 +1684,7 @@ get_completion_list :: proc(document: ^Document, position: common.Position) -> (
return list, true;
}
- if selector.uri != "" {
+ if selector.pkg != "" {
ast_context.current_package = selector.pkg;
}
@@ -1709,7 +1729,7 @@ get_completion_list :: proc(document: ^Document, position: common.Position) -> (
for name, i in v.names {
- if selector.uri != "" {
+ if selector.pkg != "" {
ast_context.current_package = selector.pkg;
}
@@ -1741,7 +1761,7 @@ get_completion_list :: proc(document: ^Document, position: common.Position) -> (
if searched, ok := index.fuzzy_search(field, {selector.pkg}); ok {
for search in searched {
- append(&symbols, search);
+ append(&symbols, search.symbol);
}
}
@@ -1764,7 +1784,7 @@ get_completion_list :: proc(document: ^Document, position: common.Position) -> (
case index.SymbolStructValue:
for name, i in s.names {
- if selector.uri != "" {
+ if selector.pkg != "" {
ast_context.current_package = selector.pkg;
}
@@ -2041,7 +2061,9 @@ fallback_position_context_completion :: proc(document: ^Document, position: comm
bracket_count -= 1;
}
- if c == ' ' || c == '{' || c == ',' || c == '}' || c == '\n' || c == '\r' {
+ if c == ' ' || c == '{' || c == ',' ||
+ c == '}' || c == '^' ||
+ c == '\n' || c == '\r' {
start = i+1;
break;
}