diff --git a/analyzer/parser/parser.v b/analyzer/parser/parser.v index 151162a7..a6c6d1c9 100644 --- a/analyzer/parser/parser.v +++ b/analyzer/parser/parser.v @@ -1,12 +1,12 @@ module parser -import tree_sitter { NodeType, Tree } +import tree_sitter as ts import os // ParseResult represents the result of a parsing operation. pub struct ParseResult { pub: - tree &Tree[NodeType] = unsafe { nil } // Resulting tree or nil if the source could not be parsed. + tree &ts.Tree[ts.NodeType] = unsafe { nil } // Resulting tree or nil if the source could not be parsed. source_text string // Source code. pub mut: path string // Path of the file that was parsed. @@ -94,9 +94,9 @@ pub fn parse_code(code string) ParseResult { // res2 = parser.parse_code_with_tree(code2, res.tree) // println(res2.tree // } -pub fn parse_code_with_tree(code string, old_tree &Tree[NodeType]) ParseResult { - mut parser := tree_sitter.new_parser[NodeType](tree_sitter.type_factory) - parser.set_language(tree_sitter.language) +pub fn parse_code_with_tree(code string, old_tree &ts.Tree[ts.NodeType]) ParseResult { + mut parser := ts.new_parser[ts.NodeType](ts.type_factory) + parser.set_language(ts.language) raw_tree := if isnil(old_tree) { unsafe { nil } } else { old_tree.raw_tree } tree := parser.parse_string(source: code, tree: raw_tree) return ParseResult{ diff --git a/analyzer/psi/AstNode.v b/analyzer/psi/AstNode.v index c74e0757..95b66e34 100644 --- a/analyzer/psi/AstNode.v +++ b/analyzer/psi/AstNode.v @@ -1,8 +1,8 @@ module psi -import tree_sitter +import tree_sitter as ts -pub fn (node AstNode) parent_of_type(typ tree_sitter.NodeType) ?AstNode { +pub fn (node AstNode) parent_of_type(typ ts.NodeType) ?AstNode { mut res := node for { res = res.parent()? diff --git a/analyzer/psi/PsiElement.v b/analyzer/psi/PsiElement.v index e9017b5c..d35ff664 100644 --- a/analyzer/psi/PsiElement.v +++ b/analyzer/psi/PsiElement.v @@ -1,10 +1,10 @@ module psi -import tree_sitter { Node, NodeType } +import tree_sitter as ts pub type ID = int -pub type AstNode = Node[NodeType] +pub type AstNode = ts.Node[ts.NodeType] pub interface PsiElement { node AstNode // base node from Tree Sitter @@ -12,7 +12,7 @@ pub interface PsiElement { stub_id StubId get_stub() ?&StubBase stub_list() &StubList - element_type() NodeType + element_type() ts.NodeType node() AstNode // return base node from Tree Sitter containing_file() &PsiFile // return file where the element is located is_equal(other PsiElement) bool // return true if the element is equal to the other element @@ -31,21 +31,21 @@ pub interface PsiElement { parent_nth(depth int) ?PsiElement // parent_of_type returns the parent node with the specified type. // If no such node exists, none is returned. - parent_of_type(typ NodeType) ?PsiElement + parent_of_type(typ ts.NodeType) ?PsiElement // parent_of_any_type returns the parent node with one of the specified types. // If no such node exists, none is returned. - parent_of_any_type(types ...NodeType) ?PsiElement + parent_of_any_type(types ...ts.NodeType) ?PsiElement // inside returns true if the node is inside a node with the specified type. - inside(typ NodeType) bool + inside(typ ts.NodeType) bool // is_parent_of returns true if the passed node is a child of the given node. is_parent_of(element PsiElement) bool // sibling_of_type_backward returns the previous node at the same nesting level with the specified type. // If no such node exists, none is returned. - sibling_of_type_backward(typ NodeType) ?PsiElement + sibling_of_type_backward(typ ts.NodeType) ?PsiElement // parent_of_type_or_self returns the parent node with the specified type, or the // node itself if its type matches the specified one. // If no such node exists, none is returned. - parent_of_type_or_self(typ NodeType) ?PsiElement + parent_of_type_or_self(typ ts.NodeType) ?PsiElement // children returns all child nodes. children() []PsiElement // named_children returns child nodes except unknown nodes. @@ -73,27 +73,27 @@ pub interface PsiElement { prev_sibling() ?PsiElement // prev_sibling_of_type returns the previous node at the same nesting level with the specified type. // If no such node exists, none is returned. - prev_sibling_of_type(typ NodeType) ?PsiElement + prev_sibling_of_type(typ ts.NodeType) ?PsiElement // prev_sibling_or_stub returns the previous node at the same nesting level or stub. // If the node is the first child node or stub, none is returned. prev_sibling_or_stub() ?PsiElement // find_child_by_type returns the first child node with the specified type. // If no such node is found, none is returned. - find_child_by_type(typ NodeType) ?PsiElement + find_child_by_type(typ ts.NodeType) ?PsiElement // has_child_of_type returns true if the node has a child with the specified type. - has_child_of_type(typ NodeType) bool + has_child_of_type(typ ts.NodeType) bool // find_child_by_type_or_stub returns the first child node with the specified type or stub. // If no such node is found, none is returned. - find_child_by_type_or_stub(typ NodeType) ?PsiElement + find_child_by_type_or_stub(typ ts.NodeType) ?PsiElement // find_child_by_name returns the first child node with the specified name. // If no such node is found, none is returned. find_child_by_name(name string) ?PsiElement // find_children_by_type returns all child nodes with the specified type. // If no such nodes are found, an empty array is returned. - find_children_by_type(typ NodeType) []PsiElement + find_children_by_type(typ ts.NodeType) []PsiElement // find_children_by_type_or_stub returns all child nodes with the specified type or stub. // If no such nodes are found, an empty array is returned. - find_children_by_type_or_stub(typ NodeType) []PsiElement + find_children_by_type_or_stub(typ ts.NodeType) []PsiElement // get_text returns the text of the node. get_text() string // text_matches returns true if the text of the node matches the specified value. diff --git a/analyzer/psi/PsiElementImpl.v b/analyzer/psi/PsiElementImpl.v index 5371eaca..6ecfc76f 100644 --- a/analyzer/psi/PsiElementImpl.v +++ b/analyzer/psi/PsiElementImpl.v @@ -1,6 +1,6 @@ module psi -import tree_sitter { NodeType } +import tree_sitter as ts pub struct PsiElementImpl { pub: @@ -48,7 +48,7 @@ pub fn (n &PsiElementImpl) stub_list() &StubList { return n.stubs_list } -pub fn (n &PsiElementImpl) element_type() NodeType { +pub fn (n &PsiElementImpl) element_type() ts.NodeType { if stub := n.get_stub() { return stub.element_type() } @@ -138,7 +138,7 @@ pub fn (n &PsiElementImpl) parent_nth(depth int) ?PsiElement { return create_element(parent, n.containing_file) } -pub fn (n &PsiElementImpl) parent_of_type(typ NodeType) ?PsiElement { +pub fn (n &PsiElementImpl) parent_of_type(typ ts.NodeType) ?PsiElement { mut res := PsiElement(n) for { res = res.parent()? @@ -150,7 +150,7 @@ pub fn (n &PsiElementImpl) parent_of_type(typ NodeType) ?PsiElement { return none } -pub fn (n &PsiElementImpl) parent_of_any_type(types ...NodeType) ?PsiElement { +pub fn (n &PsiElementImpl) parent_of_any_type(types ...ts.NodeType) ?PsiElement { mut res := PsiElement(n) for { res = res.parent()? @@ -163,7 +163,7 @@ pub fn (n &PsiElementImpl) parent_of_any_type(types ...NodeType) ?PsiElement { return none } -pub fn (n &PsiElementImpl) inside(typ NodeType) bool { +pub fn (n &PsiElementImpl) inside(typ ts.NodeType) bool { mut res := PsiElement(n) for { res = res.parent() or { return false } @@ -196,7 +196,7 @@ pub fn (n &PsiElementImpl) is_parent_of(element PsiElement) bool { return false } -pub fn (n &PsiElementImpl) sibling_of_type_backward(typ NodeType) ?PsiElement { +pub fn (n &PsiElementImpl) sibling_of_type_backward(typ ts.NodeType) ?PsiElement { mut res := PsiElement(n) for { res = res.prev_sibling_or_stub()? @@ -208,7 +208,7 @@ pub fn (n &PsiElementImpl) sibling_of_type_backward(typ NodeType) ?PsiElement { return none } -pub fn (n &PsiElementImpl) parent_of_type_or_self(typ NodeType) ?PsiElement { +pub fn (n &PsiElementImpl) parent_of_type_or_self(typ ts.NodeType) ?PsiElement { if n.node.type_name == typ { return create_element(n.node, n.containing_file) } @@ -311,7 +311,7 @@ pub fn (n &PsiElementImpl) prev_sibling() ?PsiElement { return create_element(sibling, n.containing_file) } -pub fn (n &PsiElementImpl) prev_sibling_of_type(typ NodeType) ?PsiElement { +pub fn (n &PsiElementImpl) prev_sibling_of_type(typ ts.NodeType) ?PsiElement { mut res := PsiElement(n) for { res = res.prev_sibling_or_stub()? @@ -335,12 +335,12 @@ pub fn (n &PsiElementImpl) prev_sibling_or_stub() ?PsiElement { return n.prev_sibling() } -pub fn (n &PsiElementImpl) find_child_by_type(typ NodeType) ?PsiElement { +pub fn (n &PsiElementImpl) find_child_by_type(typ ts.NodeType) ?PsiElement { ast_node := n.node.first_node_by_type(typ)? return create_element(ast_node, n.containing_file) } -pub fn (n &PsiElementImpl) has_child_of_type(typ NodeType) bool { +pub fn (n &PsiElementImpl) has_child_of_type(typ ts.NodeType) bool { if stub := n.get_stub() { return stub.has_child_of_type(node_type_to_stub_type(typ)) } @@ -352,7 +352,7 @@ pub fn (n &PsiElementImpl) has_child_of_type(typ NodeType) bool { return false } -pub fn (n &PsiElementImpl) find_child_by_type_or_stub(typ NodeType) ?PsiElement { +pub fn (n &PsiElementImpl) find_child_by_type_or_stub(typ ts.NodeType) ?PsiElement { if stub := n.get_stub() { child := stub.get_child_by_type(node_type_to_stub_type(typ))? return child.get_psi() @@ -367,7 +367,7 @@ pub fn (n &PsiElementImpl) find_child_by_name(name string) ?PsiElement { return create_element(ast_node, n.containing_file) } -pub fn (n &PsiElementImpl) find_children_by_type(typ NodeType) []PsiElement { +pub fn (n &PsiElementImpl) find_children_by_type(typ ts.NodeType) []PsiElement { mut result := []PsiElement{} mut child := n.node.first_child() or { return [] } for { @@ -379,7 +379,7 @@ pub fn (n &PsiElementImpl) find_children_by_type(typ NodeType) []PsiElement { return result } -pub fn (n &PsiElementImpl) find_children_by_type_or_stub(typ NodeType) []PsiElement { +pub fn (n &PsiElementImpl) find_children_by_type_or_stub(typ ts.NodeType) []PsiElement { if stub := n.get_stub() { return stub.get_children_by_type(node_type_to_stub_type(typ)).get_psi() } @@ -395,7 +395,7 @@ pub fn (n &PsiElementImpl) find_children_by_type_or_stub(typ NodeType) []PsiElem return result } -pub fn (n &PsiElementImpl) find_last_child_by_type(typ NodeType) ?PsiElement { +pub fn (n &PsiElementImpl) find_last_child_by_type(typ ts.NodeType) ?PsiElement { ast_node := n.node.last_node_by_type(typ)? return create_element(ast_node, n.containing_file) } diff --git a/analyzer/psi/PsiFile.v b/analyzer/psi/PsiFile.v index 2378f9f8..aa734fa6 100644 --- a/analyzer/psi/PsiFile.v +++ b/analyzer/psi/PsiFile.v @@ -4,7 +4,7 @@ import lsp import time import utils import loglib -import tree_sitter { NodeType, Tree } +import tree_sitter as ts import analyzer.parser @[heap] @@ -13,12 +13,12 @@ pub: path string stub_list &StubList = unsafe { nil } pub mut: - tree &Tree[NodeType] = unsafe { nil } + tree &ts.Tree[ts.NodeType] = unsafe { nil } source_text string root PsiElement } -pub fn new_psi_file(path string, tree &Tree[NodeType], source_text string) &PsiFile { +pub fn new_psi_file(path string, tree &ts.Tree[ts.NodeType], source_text string) &PsiFile { mut file := &PsiFile{ path: path tree: unsafe { tree } diff --git a/analyzer/psi/PsiNamedElement.v b/analyzer/psi/PsiNamedElement.v index f2968dcc..9f902cea 100644 --- a/analyzer/psi/PsiNamedElement.v +++ b/analyzer/psi/PsiNamedElement.v @@ -1,9 +1,9 @@ module psi -import tree_sitter +import tree_sitter as ts pub interface PsiNamedElement { - parent_of_type(typ tree_sitter.NodeType) ?PsiElement + parent_of_type(typ ts.NodeType) ?PsiElement identifier_text_range() TextRange identifier() ?PsiElement name() string diff --git a/analyzer/psi/StubBase.v b/analyzer/psi/StubBase.v index 0e90d758..defa9864 100644 --- a/analyzer/psi/StubBase.v +++ b/analyzer/psi/StubBase.v @@ -1,6 +1,6 @@ module psi -import tree_sitter +import tree_sitter as ts pub type StubId = int @@ -74,7 +74,7 @@ pub fn (s &StubBase) stub_type() StubType { return s.stub_type } -pub fn (s &StubBase) element_type() tree_sitter.NodeType { +pub fn (s &StubBase) element_type() ts.NodeType { return match s.stub_type { .root { .unknown } .function_declaration { .function_declaration } diff --git a/analyzer/psi/StubbedElementTypeImpl.v b/analyzer/psi/StubbedElementTypeImpl.v index cec71816..bb13e5f5 100644 --- a/analyzer/psi/StubbedElementTypeImpl.v +++ b/analyzer/psi/StubbedElementTypeImpl.v @@ -1,7 +1,7 @@ module psi import utils -import tree_sitter +import tree_sitter as ts pub enum StubType as u8 { root @@ -60,7 +60,7 @@ pub enum StubType as u8 { embedded_definition } -pub fn node_type_to_stub_type(typ tree_sitter.NodeType) StubType { +pub fn node_type_to_stub_type(typ ts.NodeType) StubType { return match typ { .function_declaration { .function_declaration } .receiver { .receiver } diff --git a/analyzer/psi/TreeWalker.v b/analyzer/psi/TreeWalker.v index ad687e28..ecc63cd7 100644 --- a/analyzer/psi/TreeWalker.v +++ b/analyzer/psi/TreeWalker.v @@ -1,11 +1,11 @@ module psi -import tree_sitter +import tree_sitter as ts struct TreeWalker { mut: already_visited_children bool - cursor tree_sitter.TreeCursor[tree_sitter.NodeType] @[required] + cursor ts.TreeCursor[ts.NodeType] @[required] } pub fn (mut tw TreeWalker) next() ?AstNode { diff --git a/tree_sitter/examples/cursor.v b/tree_sitter/examples/cursor.v index f22ca5fb..8b1607c5 100644 --- a/tree_sitter/examples/cursor.v +++ b/tree_sitter/examples/cursor.v @@ -1,10 +1,10 @@ module main -import tree_sitter +import tree_sitter as ts fn main() { - mut p := tree_sitter.new_parser[tree_sitter.NodeType](ts.type_factory) - p.set_language(tree_sitter.language) + mut p := ts.new_parser[ts.NodeType](ts.type_factory) + p.set_language(ts.language) code := ' fn foo() int { diff --git a/tree_sitter/examples/simple.v b/tree_sitter/examples/simple.v index be0ae378..fa6401a5 100644 --- a/tree_sitter/examples/simple.v +++ b/tree_sitter/examples/simple.v @@ -1,10 +1,10 @@ module main -import tree_sitter +import tree_sitter as ts fn main() { - mut p := tree_sitter.new_parser[tree_sitter.NodeType](ts.type_factory) - p.set_language(tree_sitter.language) + mut p := ts.new_parser[ts.NodeType](ts.type_factory) + p.set_language(ts.language) code := 'fn main() {}' tree := p.parse_string(source: code) diff --git a/tree_sitter/examples/with_old_tree.v b/tree_sitter/examples/with_old_tree.v index 3215bf26..ecf70adf 100644 --- a/tree_sitter/examples/with_old_tree.v +++ b/tree_sitter/examples/with_old_tree.v @@ -1,11 +1,11 @@ module main import time -import tree_sitter +import tree_sitter as ts fn main() { - mut p := tree_sitter.new_parser[tree_sitter.NodeType](tree_sitter.type_factory) - p.set_language(tree_sitter.language) + mut p := ts.new_parser[ts.NodeType](ts.type_factory) + p.set_language(ts.language) code := ' fn foo() int { diff --git a/tree_sitter/simple_test.v b/tree_sitter/simple_test.v index 541b4d0b..b2981080 100644 --- a/tree_sitter/simple_test.v +++ b/tree_sitter/simple_test.v @@ -1,8 +1,8 @@ -import tree_sitter +import tree_sitter as ts fn test_simple() { - mut p := tree_sitter.new_parser[tree_sitter.NodeType](tree_sitter.type_factory) - p.set_language(tree_sitter.language) + mut p := ts.new_parser[ts.NodeType](ts.type_factory) + p.set_language(ts.language) code := 'fn main() {}' tree := p.parse_string(source: code) diff --git a/tree_sitter/tools/project-checker.v b/tree_sitter/tools/project-checker.v index 9e01ac7e..669f4c45 100644 --- a/tree_sitter/tools/project-checker.v +++ b/tree_sitter/tools/project-checker.v @@ -6,7 +6,7 @@ import sync import os import time import analyzer.parser -import tree_sitter +import tree_sitter as ts fn main() { mut checker := Checker{ @@ -15,7 +15,7 @@ fn main() { checker.check() } -pub type AstNode = tree_sitter.Node[tree_sitter.NodeType] +pub type AstNode = ts.Node[ts.NodeType] struct ErrorInfo { path string