Skip to content

Commit

Permalink
Add scopes symbol navigation (#189)
Browse files Browse the repository at this point in the history
* Add meta.function-call.powershell scope to function calls

This enables Sublime Text to correctly find references to functions being called,
at least for functions written according to the recommended style.

* Define 'class-method' context in syntax file

Static and instance methods are now identified as functions.
F12 and shift+F12 can now correctly find definitions and references of class methods.

* Fix wrong scope name in 'class-method' context
  • Loading branch information
matteocoder authored Jan 11, 2025
1 parent a243f8b commit 7b9767e
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 72 deletions.
17 changes: 16 additions & 1 deletion PowerShell.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ contexts:
- include: script-block
- include: escape-characters
- include: numeric-constant
- include: class-method
- match: (@)(\()
captures:
1: keyword.other.array.begin.powershell
Expand Down Expand Up @@ -342,7 +343,7 @@ contexts:
\-\w+? # Any "noun"
(?:\.(?i:exe|cmd|bat|ps1))?\b # More path stuff
)
scope: support.function.powershell
scope: meta.function-call.powershell support.function.powershell
# Builtin cmdlets with reserved verbs
- match: \b(?i:(?:foreach|where|sort|tee)-object)\b
scope: support.function.powershell
Expand Down Expand Up @@ -793,3 +794,17 @@ contexts:
3: support.variable.drive.powershell
4: variable.other.readwrite.powershell
5: punctuation.section.braces.end

class-method:
- match: ^(?:\s*)(?i)(hidden|static)?\s*(\[)((?!\d+|\.)[\p{L}\p{N}.]+)(\])\s*((?:\p{L}|\d|_|-|\.)+)\s*(?=\()
scope: meta.function.powershell
captures:
1: storage.modifier.powershell
2: punctuation.section.brackets.begin.powershell
3: storage.type.powershell
4: punctuation.section.brackets.end.powershell
5: entity.name.function.powershell
push:
- match: (?=\()
pop: true
- include: comment-line
10 changes: 10 additions & 0 deletions Tests/syntax_test_Class.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class TypeName
# ^ punctuation.section.brackets.begin
# ^^^^ storage.type
# ^ punctuation.section.brackets.end
# ^^^^^^^^^^^^^ entity.name.function
# @@@@@@@@@@@@@ definition
# ^ punctuation.section.group.begin
# ^ punctuation.section.brackets.begin
# ^^^^^^^^^ storage.type
Expand All @@ -105,6 +107,8 @@ class TypeName
# <- punctuation.section.brackets.begin
# ^ storage.type
# ^ punctuation.section.brackets.end
# ^^^^^^^^^^^^^ entity.name.function
# @@@@@@@@@@@@@ definition
# ^ punctuation.section.group.begin
# ^ punctuation.section.brackets.begin
# ^^^ storage.type
Expand All @@ -126,3 +130,9 @@ class TypeName
# ^^ variable.other.member
}
}

[TypeName]::MemberMethod1()
# @@@@@@@@@@@@@ reference

$object.MemberMethod2()
# @@@@@@@@@@@@@ reference
3 changes: 3 additions & 0 deletions Tests/syntax_test_Function.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ using module Microsoft.Management
# ^ keyword.other
# ^ variable.parameter

function Test-Path {}
# @@@@@@@@@ definition
<#
# <- punctuation.definition.comment.block.begin
.Synopsis
Expand Down Expand Up @@ -321,6 +323,7 @@ function Verb-Noun {
# ^ meta.attribute punctuation.section.group.end
# ^ meta.attribute punctuation.section.brackets.end
[ValidateScript({Test-Path $_})] #Make sure cmdlets don't break highlighting
# @@@@@@@@@ reference
# <- meta.attribute punctuation.section.brackets.begin
# ^ meta.attribute support.function.attribute
# ^ meta.attribute punctuation.section.group.begin
Expand Down
Loading

0 comments on commit 7b9767e

Please sign in to comment.