Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/tree_sitter_v.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ jobs:
- name: Run tests
run: npm run test

- name: Lint
run: npm run lint

test-bindings:
runs-on: ubuntu-latest

Expand Down
1 change: 0 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[submodule "tree_sitter_v/bindings/core"]
path = tree_sitter_v/bindings/core
url = https://github.com/tree-sitter/tree-sitter.git
branch = master
3 changes: 3 additions & 0 deletions src/analyzer/psi/GlobalVarDefinition.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub fn (_ &GlobalVarDefinition) is_public() bool {
}

pub fn (n &GlobalVarDefinition) identifier() ?PsiElement {
if node := n.find_child_by_name('name') {
return node
}
return n.find_child_by_type(.identifier)
}

Expand Down
29 changes: 21 additions & 8 deletions src/analyzer/psi/PsiFile.v
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,29 @@ pub fn (p &PsiFile) module_clause() ?&ModuleClause {
}

pub fn (p &PsiFile) get_imports() []ImportSpec {
import_list := p.root().find_child_by_type_or_stub(.import_list) or { return [] }
declarations := import_list.find_children_by_type_or_stub(.import_declaration)
mut import_specs := []ImportSpec{cap: declarations.len}
for declaration in declarations {
spec := declaration.find_child_by_type_or_stub(.import_spec) or { continue }
if spec is ImportSpec {
import_specs << spec
mut specs := []ImportSpec{}

if p.is_stub_based() {
for _, stub in p.stub_list.index_map {
if stub.stub_type == .import_spec {
if element := stub.get_psi() {
if element is ImportSpec {
specs << element
}
}
}
}
} else {
mut walker := new_psi_tree_walker(p.root())
for {
child := walker.next() or { break }
if child is ImportSpec {
specs << child
}
}
}
return import_specs

return specs
}

pub fn (p &PsiFile) resolve_import_spec(name string) ?ImportSpec {
Expand Down
264 changes: 170 additions & 94 deletions src/server/semantic/DumbAwareSemanticVisitor.v
Original file line number Diff line number Diff line change
Expand Up @@ -44,128 +44,204 @@ fn (_ DumbAwareSemanticVisitor) highlight_node(node psi.AstNode, root psi.PsiEle
containing_file := root.containing_file() or { return }
source_text := containing_file.source_text

if node.type_name == .enum_field_definition {
if first_child := node.first_child() {
result << element_to_semantic(first_child, .enum_member)
}
} else if node.type_name == .field_name {
result << element_to_semantic(node, .property)
} else if node.type_name == .range_clause {
if first_child := node.first_child() {
result << element_to_semantic(first_child, .property)
}
} else if node.type_name == .struct_field_declaration {
if first_child := node.first_child() {
if first_child.type_name != .embedded_definition {
match node.type_name {
.enum_field_definition {
if first_child := node.first_child() {
result << element_to_semantic(first_child, .enum_member)
}
}
.field_name {
result << element_to_semantic(node, .property)
}
.range_clause {
if first_child := node.first_child() {
result << element_to_semantic(first_child, .property)
}
}
} else if node.type_name == .module_clause {
if last_child := node.last_child() {
result << element_to_semantic(last_child, .namespace)
.struct_field_declaration {
if first_child := node.first_child() {
if first_child.type_name != .embedded_definition {
result << element_to_semantic(first_child, .property)
}
}
}
.module_clause {
if last_child := node.last_child() {
result << element_to_semantic(last_child, .namespace)
}
}
.attribute {
// '['
if first_child := node.first_child() {
result << element_to_semantic(first_child, .decorator)
}
// ']'
if last_child := node.last_child() {
result << element_to_semantic(last_child, .decorator)
}
}
.key_value_attribute {
if value_child := node.child_by_field_name('value') {
if value_child.type_name == .identifier {
result << element_to_semantic(value_child, .string)
}
}
}
} else if node.type_name == .attribute {
// '['
if first_child := node.first_child() {
result << element_to_semantic(first_child, .decorator)
.qualified_type {
if first_child := node.first_child() {
result << element_to_semantic(first_child, .namespace)
}
if last_child := node.last_child() {
result << element_to_semantic(last_child, .type_)
}
}
// ']'
if last_child := node.last_child() {
result << element_to_semantic(last_child, .decorator)
.unknown {
text := node.text(source_text)
if text == 'sql' {
if parent := node.parent() {
if parent.type_name == .sql_expression {
result << element_to_semantic(node, .keyword)
}
}
} else if text == 'chan' {
if parent := node.parent() {
if parent.type_name == .channel_type {
result << element_to_semantic(node, .keyword)
}
}
} else if text == 'thread' {
if parent := node.parent() {
if parent.type_name == .thread_type {
result << element_to_semantic(node, .keyword)
}
}
}
}
} else if node.type_name == .key_value_attribute {
if value_child := node.child_by_field_name('value') {
if value_child.type_name == .identifier {
result << element_to_semantic(value_child, .string)
.enum_declaration {
if identifier := node.child_by_field_name('name') {
result << element_to_semantic(identifier, .enum_)
}
}
} else if node.type_name == .qualified_type {
if first_child := node.first_child() {
result << element_to_semantic(first_child, .namespace)
.implements_clause {
if !node.is_named() {
result << element_to_semantic(node, .keyword)
}
}
if last_child := node.last_child() {
result << element_to_semantic(last_child, .type_)
.interface_declaration {
if identifier := node.child_by_field_name('name') {
result << element_to_semantic(identifier, .interface_)
}
}
} else if node.type_name == .unknown {
text := node.text(source_text)

if text == 'sql' {
if parent := node.parent() {
if parent.type_name == .sql_expression {
result << element_to_semantic(node, .keyword)
.parameter_declaration, .receiver {
if identifier := node.child_by_field_name('name') {
if _ := node.child_by_field_name('mutability') {
result << element_to_semantic(identifier, .parameter, 'mutable')
} else {
result << element_to_semantic(identifier, .parameter)
}
}
}
if text == 'chan' {
if parent := node.parent() {
if parent.type_name == .channel_type {
result << element_to_semantic(node, .keyword)
.reference_expression {
def := psi.node_to_var_definition(node, containing_file, none)
if !isnil(def) {
if def.is_mutable() {
result << element_to_semantic(node, .variable, 'mutable')
} else {
result << element_to_semantic(node, .variable)
}
}

first_char := node.first_char(source_text)
if first_char == `@` || first_char == `$` {
result << element_to_semantic(node, .property) // not a best variant...
}
}
.const_definition {
if name := node.child_by_field_name('name') {
result << element_to_semantic(name, .property) // not a best variant...
}
}
if text == 'thread' {
if parent := node.parent() {
if parent.type_name == .thread_type {
result << element_to_semantic(node, .keyword)
.import_path {
count := node.child_count()
for i in 0 .. count {
if child := node.child(i) {
if child.type_name == .import_name {
result << element_to_semantic(child, .namespace)
}
}
}
}
} else if node.type_name == .enum_declaration {
if identifier := node.child_by_field_name('name') {
result << element_to_semantic(identifier, .enum_)
.import_alias {
if last_child := node.last_child() {
result << element_to_semantic(last_child, .namespace)
}
}
} else if node.type_name == .parameter_declaration || node.type_name == .receiver {
if identifier := node.child_by_field_name('name') {
if _ := node.child_by_field_name('mutability') {
result << element_to_semantic(identifier, .parameter, 'mutable')
} else {
result << element_to_semantic(identifier, .parameter)
.compile_time_if_expression {
if condition := node.child_by_field_name('condition') {
highlight_compile_time_condition(condition, mut result)
}
}
} else if node.type_name == .reference_expression {
def := psi.node_to_var_definition(node, containing_file, none)
if !isnil(def) {
if def.is_mutable() {
result << element_to_semantic(node, .variable, 'mutable')
} else {
result << element_to_semantic(node, .variable)
.asm_statement {
if first := node.first_child() {
result << element_to_semantic(first, .keyword)
}
if modifier := node.child_by_field_name('modifiers') {
result << element_to_semantic(modifier, .keyword)
}
if arch := node.child_by_field_name('arch') {
result << element_to_semantic(arch, .variable, 'readonly', 'defaultLibrary')
}
}

first_char := node.first_char(source_text)
if first_char == `@` || first_char == `$` {
result << element_to_semantic(node, .property) // not a best variant...
}
} else if node.type_name == .const_definition {
if name := node.child_by_field_name('name') {
result << element_to_semantic(name, .property) // not a best variant...
}
} else if node.type_name == .import_path {
if last_part := node.last_child() {
result << element_to_semantic(last_part, .namespace)
}
} else if node.type_name in [.interpolation_opening, .interpolation_closing] {
result << element_to_semantic(node, .keyword)
} else if node.type_name == .generic_parameter {
result << element_to_semantic(node, .type_parameter)
} else if node.type_name == .global_var_definition {
if identifier := node.child_by_field_name('name') {
result << element_to_semantic(identifier, .variable, 'global')
}
} else if node.type_name == .function_declaration {
if first_child := node.child_by_field_name('name') {
first_char := first_child.first_char(source_text)
if first_char in [`@`, `$`] {
// tweak highlighting for @lock/@rlock
result << element_to_semantic(first_child, .function)
.interpolation_opening, .interpolation_closing {
result << element_to_semantic(node, .keyword)
}
.generic_parameter {
result << element_to_semantic(node, .type_parameter)
}
.variadic_parameter {
result << element_to_semantic(node, .operator)
}
.global_var_definition {
if identifier := node.child_by_field_name('name') {
result << element_to_semantic(identifier, .variable, 'global')
}
if modifiers := node.child_by_field_name('modifiers') {
result << element_to_semantic(modifiers, .keyword)
}
}
.function_declaration {
if first_child := node.child_by_field_name('name') {
first_char := first_child.first_char(source_text)
if first_char in [`@`, `$`] {
// tweak highlighting for @lock/@rlock
result << element_to_semantic(first_child, .function)
}
}
}
else {
$if debug {
// this useful for finding errors in parsing
if node.type_name == .error {
result << element_to_semantic(node, .namespace, 'mutable')
}
}
}
}
}

$if debug {
// this useful for finding errors in parsing
if node.type_name == .error {
result << element_to_semantic(node, .namespace, 'mutable')
fn highlight_compile_time_condition(node psi.AstNode, mut result []SemanticToken) {
if node.type_name == .reference_expression {
result << element_to_semantic(node, .variable, 'readonly', 'defaultLibrary')
} else if node.type_name == .binary_expression || node.type_name == .unary_expression {
count := node.child_count()
for i in 0 .. count {
if child := node.child(i) {
highlight_compile_time_condition(child, mut result)
}
}
} else if node.type_name == .parenthesized_expression {
if child := node.child(1) {
highlight_compile_time_condition(child, mut result)
}
}
}
17 changes: 17 additions & 0 deletions src/tests/definitions.v
Original file line number Diff line number Diff line change
Expand Up @@ -371,5 +371,22 @@ fn definitions() testing.Tester {
t.assert_uri_from_stubs(first.target_uri, 'implicit.v')!
})

t.test('global variable with volatile modifier', fn (mut t testing.Test, mut fixture testing.Fixture) ! {
fixture.configure_by_text('1.v', '
__global volatile base_revision = 0

fn main() {
println(base/*caret*/_revision)
}
'.trim_indent())!

locations := fixture.definition_at_cursor()
t.assert_has_definition(locations)!

first := locations.first()
t.assert_uri(first.target_uri, fixture.current_file_uri())!
t.assert_definition_name(first, 'base_revision')!
})

return t
}
Loading
Loading