Skip to content

Commit

Permalink
Define 'class-method' context in syntax file
Browse files Browse the repository at this point in the history
Static and instance methods are now identified as functions.
F12 and shift+F12 can now correctly find definitions and references of class methods.
  • Loading branch information
matteocoder committed Jan 10, 2025
1 parent 290970e commit e8dce0c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 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 @@ -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.braces.begin.powershell
3: storage.type.powershell
4: punctuation.section.braces.end.powershell
5: entity.name.function.powershell
push:
- match: (?=\()
pop: true
- include: comment-line
19 changes: 19 additions & 0 deletions Tests/syntax_test_PowerShell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,9 @@ function New-File ($Name) { }
# @@@@@@@@ definition
# ^ punctuation.section.braces.begin
# ^ punctuation.section.braces.end
New-File
#^^^^^^^^ meta.function-call support.function
#@@@@@@@@ reference
function NewFile($Name) { }
# <- storage.type
# ^^^^^^^ entity.name.function
Expand Down Expand Up @@ -1129,6 +1132,8 @@ class Vehicle {

[void]Drive([int]$NumberOfMiles) {
# ^^^^ storage.type
# ^^^^^ meta.function entity.name.function
# @@@@@ definition
# ^^^ storage.type
# ^ punctuation.definition.variable
# ^ variable.other.readwrite
Expand All @@ -1140,9 +1145,23 @@ class Vehicle {

}
# <- punctuation.section.braces.end
static [System.Array] GetAvailableColors() {
# ^^^^^^^^^^^^^^^^^^ meta.function entity.name.function
# @@@@@@@@@@@@@@@@@@ definition
# ^^^^^^ meta.function storage.modifier
# ^^^^^^^^^^^ meta.function storage.type
return 'yellow', 'red'
}
}
# <- punctuation.section.braces.end

$fiat.Drive(42)
# ^^^^^ meta.function-call
# @@@@@ reference
[Vehicle]::GetAvailableColors()
# ^^^^^^^^^^^^^^^^^^ meta.function-call variable.function
# @@@@@@@@@@@@@@@@@@ reference

# Control words
foreach ($item in $collection) {
# <- keyword.control
Expand Down

0 comments on commit e8dce0c

Please sign in to comment.