Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
#30 children handling for blocks, improved seantic highlighting for i…
Browse files Browse the repository at this point in the history
…mpls
  • Loading branch information
musab-mah-7 committed Mar 1, 2024
1 parent 70dde26 commit a4faab3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 28 deletions.
82 changes: 59 additions & 23 deletions bundled/tool/common/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
Architype,
HasVar,
ParamVar,
AbilityDef,
EnumDef,
ArchDef,
IfStmt,
ElseStmt,
ElseIf,
WhileStmt,
WithStmt,
IterForStmt,
Expand Down Expand Up @@ -107,8 +112,17 @@ def __init__(

@property
def do_skip(self):
return isinstance(self.node, (IfStmt, WhileStmt, WithStmt, IterForStmt, InForStmt))
return isinstance(self.node, (IfStmt, ElseStmt, ElseIf, WhileStmt, WithStmt, IterForStmt, InForStmt))

''''
When there's an include, file will have the AST nodes from the included file.
For sementic higlighting, hover information etc.. we don't need this nodes to be included..
so we need to keep track of the origin file of the node.
'''
@property
def node_origin_file(self):
return f"file://{os.path.join(os.getcwd(), self.node.loc.mod_path)}"

@property
def sym_name(self):
return self.node.sym_name
Expand Down Expand Up @@ -233,29 +247,51 @@ def location(self):
@property
def children(self):
if hasattr(self, "sym_tab"):
for kid_sym_tab in self.sym_tab.kid:
if isinstance(
kid_sym_tab.owner,
(IfStmt, WhileStmt, WithStmt, IterForStmt),
):
for kid_sym in kid_sym_tab.tab.values():
kid_symbol = Symbol(kid_sym, self.doc_uri)
yield kid_symbol
continue
kid_symbol = Symbol(kid_sym_tab, self.doc_uri)
yield from self._yield_direct_children_symbol(self.sym_tab.tab.values())
for symbol_tab in self.sym_tab.kid:
yield from self._yield_nested_block_children(symbol_tab)
# vars = (
# self.node.get_all_sub_nodes(HasVar)
# if isinstance(self.node, Architype)
# else (
# self.node.get_all_sub_nodes(ParamVar)
# if isinstance(self.node, Ability)
# else []
# )
# )
# for var in vars:
# var_symbol = Symbol(var, self.doc_uri)
# yield var_symbol
def _yield_direct_children_symbol(self, symbols):
for sym in symbols:
yield Symbol(sym, self.doc_uri)

def _yield_nested_block_children(self, kid_sym_tab):
if isinstance(
kid_sym_tab.owner,
(InForStmt,
IfStmt,
ElseStmt,
ElseIf,
WhileStmt,
WithStmt,
IterForStmt,
AbilityDef,
EnumDef,
ArchDef,
Ability,
Architype,
HasVar,
ParamVar),
):
for kid_sym in kid_sym_tab.tab.values():
kid_symbol = Symbol(kid_sym, self.doc_uri)
yield kid_symbol
vars = (
self.node.get_all_sub_nodes(HasVar)
if isinstance(self.node, Architype)
else (
self.node.get_all_sub_nodes(ParamVar)
if isinstance(self.node, Ability)
else []
)
)
for var in vars:
var_symbol = Symbol(var, self.doc_uri)
yield var_symbol
for kid_sym in kid_sym_tab.uses:
if hasattr(kid_sym, "name") and kid_sym.name == "NAME":
yield Symbol(kid_sym, self.doc_uri)
kid_symbol = Symbol(kid_sym_tab, self.doc_uri)
yield kid_symbol

def uses(self, ls: LanguageServer) -> List["Symbol"]:
for mod_url in ls.jlws.modules.keys():
Expand Down
16 changes: 13 additions & 3 deletions bundled/tool/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def get_all_children(
ls: LanguageServer, sym: Symbol, return_uses: bool = False
) -> list[Symbol]:
for child in sym.children:
yield child
if not child.do_skip:
yield child
if return_uses:
yield from child.uses(ls)
yield from get_all_children(ls, child)
Expand Down Expand Up @@ -183,10 +184,10 @@ def get_all_symbols(
all_symbols = []
all_symbols.extend(doc.symbols)
all_symbols.extend(doc.use_symbols)
for sym in doc.symbols:
for sym in all_symbols:
if not include_impl and sym.sym_type == "impl":
continue
if sym.do_skip:
if sym.do_skip or (include_impl and sym.sym_type == "impl"):
yield from get_all_children(ls, sym, True)
continue
yield sym
Expand All @@ -200,6 +201,15 @@ def get_all_symbols(
yield sym
yield from sym.uses(ls)

def extract_current_doc_symbols(
ls: LanguageServer,
doc: TextDocumentItem,
include_dep: bool = True,
include_impl: bool = False,
) -> list[Symbol]:
all_symbols = list(get_all_symbols(ls, doc, include_dep, include_impl))
return [sym for sym in all_symbols if sym.node_origin_file == doc.uri]


def get_scope_at_pos(
ls: LanguageServer, doc: TextDocumentItem, pos: Position, symbols: list[Symbol]
Expand Down
5 changes: 3 additions & 2 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def update_sys_path(path_to_add: str, strategy: str) -> None:
get_command,
sort_chunks_relative_to_previous,
flatten_chunks,
extract_current_doc_symbols
)


Expand Down Expand Up @@ -330,7 +331,7 @@ def hover(ls, params: lsp.HoverParams) -> Optional[lsp.Hover]:
uri = params.text_document.uri
position = params.position
lsp_document = ls.workspace.get_text_document(uri)
all_symbols = list(get_all_symbols(ls, lsp_document, True, True))
all_symbols = list(extract_current_doc_symbols(ls, lsp_document, True, True))
log_to_output(ls, f"symbols: {all_symbols}")
return get_hover_info(ls, lsp_document, position)

Expand Down Expand Up @@ -372,7 +373,7 @@ def semantic_tokens_full(ls, params: lsp.SemanticTokensParams) -> lsp.SemanticTo
doc = ls.workspace.get_text_document(uri)
if not hasattr(doc, "symbols"):
update_doc_tree(ls, doc.uri)
symbols = get_all_symbols(ls, doc, True, True)
symbols = extract_current_doc_symbols(ls, doc, True, True)
data = []
for sym in symbols:
if sym.doc_uri != doc.uri:
Expand Down

0 comments on commit a4faab3

Please sign in to comment.