diff options
| author | Andrey Chalkin <L2jLiga@gmail.com> | 2024-07-22 23:14:45 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-22 23:14:45 +0200 |
| commit | c5b3560306c93498cd745f597880d4afe95ec5c8 (patch) | |
| tree | 881542851e021eeba4ce9eb551fc96a492c133b7 | |
| parent | refactor(latex): deduplicate captures, reorder (diff) | |
| download | nvim-treesitter-c5b3560306c93498cd745f597880d4afe95ec5c8.tar nvim-treesitter-c5b3560306c93498cd745f597880d4afe95ec5c8.tar.gz nvim-treesitter-c5b3560306c93498cd745f597880d4afe95ec5c8.tar.bz2 nvim-treesitter-c5b3560306c93498cd745f597880d4afe95ec5c8.tar.lz nvim-treesitter-c5b3560306c93498cd745f597880d4afe95ec5c8.tar.xz nvim-treesitter-c5b3560306c93498cd745f597880d4afe95ec5c8.tar.zst nvim-treesitter-c5b3560306c93498cd745f597880d4afe95ec5c8.zip | |
feat: add powershell language
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | lockfile.json | 3 | ||||
| -rw-r--r-- | lua/nvim-treesitter/parsers.lua | 9 | ||||
| -rw-r--r-- | queries/powershell/folds.scm | 13 | ||||
| -rw-r--r-- | queries/powershell/highlights.scm | 325 | ||||
| -rw-r--r-- | queries/powershell/indents.scm | 41 | ||||
| -rw-r--r-- | queries/powershell/injections.scm | 27 | ||||
| -rw-r--r-- | queries/powershell/locals.scm | 81 |
8 files changed, 500 insertions, 0 deletions
@@ -351,6 +351,7 @@ We are looking for maintainers to add more parsers and to write query files for - [x] [pod](https://github.com/tree-sitter-perl/tree-sitter-pod) (maintained by @RabbiVeesh, @LeoNerd) - [x] [Path of Exile item filter](https://github.com/ObserverOfTime/tree-sitter-poe-filter) (experimental, maintained by @ObserverOfTime) - [x] [pony](https://github.com/amaanq/tree-sitter-pony) (maintained by @amaanq, @mfelsche) +- [x] [powershell](https://github.com/airbus-cert/tree-sitter-powershell) (maintained by @L2jLiga) - [x] [printf](https://github.com/ObserverOfTime/tree-sitter-printf) (maintained by @ObserverOfTime) - [x] [prisma](https://github.com/victorhqc/tree-sitter-prisma) (maintained by @elianiva) - [x] [problog](https://github.com/foxyseta/tree-sitter-prolog) (maintained by @foxyseta) diff --git a/lockfile.json b/lockfile.json index c5c4f0f34..0b5531d40 100644 --- a/lockfile.json +++ b/lockfile.json @@ -533,6 +533,9 @@ "pony": { "revision": "73ff874ae4c9e9b45462673cbc0a1e350e2522a7" }, + "powershell": { + "revision": "804d86fd4ad286bd0cc1c1f0f7b28bd7af6755ad" + }, "printf": { "revision": "0e0aceabbf607ea09e03562f5d8a56f048ddea3d" }, diff --git a/lua/nvim-treesitter/parsers.lua b/lua/nvim-treesitter/parsers.lua index 906160ed7..1cdb1b186 100644 --- a/lua/nvim-treesitter/parsers.lua +++ b/lua/nvim-treesitter/parsers.lua @@ -1562,6 +1562,15 @@ list.pony = { maintainers = { "@amaanq", "@mfelsche" }, } +list.powershell = { + install_info = { + url = "https://github.com/airbus-cert/tree-sitter-powershell", + files = { "src/parser.c", "src/scanner.c" }, + }, + filetype = "ps1", + maintainers = { "L2jLiga" }, +} + list.printf = { install_info = { url = "https://github.com/ObserverOfTime/tree-sitter-printf", diff --git a/queries/powershell/folds.scm b/queries/powershell/folds.scm new file mode 100644 index 000000000..d87e3f688 --- /dev/null +++ b/queries/powershell/folds.scm @@ -0,0 +1,13 @@ +[ + (expandable_here_string_literal) + (verbatim_here_string_characters) + (function_statement) + (param_block) + (script_block_expression) + (statement_block) + (switch_body) + (array_expression) + (hash_literal_expression) + (class_statement) + (class_method_definition) +] @fold diff --git a/queries/powershell/highlights.scm b/queries/powershell/highlights.scm new file mode 100644 index 000000000..851e69a47 --- /dev/null +++ b/queries/powershell/highlights.scm @@ -0,0 +1,325 @@ +; Punctuation +;------------ +[ + "(" + ")" + "[" + "]" + "{" + "}" +] @punctuation.bracket + +(hash_literal_expression + [ + "@{" + "}" + ] @punctuation.special) + +(array_expression + [ + "@(" + ")" + ] @punctuation.special) + +[ + "." + "::" + "," + ";" + (empty_statement) + (command_argument_sep) +] @punctuation.delimiter + +; Keywords +;--------- +[ + "if" + "elseif" + "else" + "switch" +] @keyword.conditional + +[ + "foreach" + "for" + "while" + "do" + "until" + "in" + "break" + "continue" +] @keyword.repeat + +[ + "function" + "filter" + "workflow" + "exit" + "trap" + "param" + "inlinescript" +] @keyword.function + +"return" @keyword.return + +[ + "class" + "enum" +] @keyword.type + +(class_attribute) @keyword.modifier + +[ + "throw" + "try" + "catch" + "finally" +] @keyword.exception + +[ + "parallel" + "sequence" +] @keyword.coroutine + +[ + "param" + "dynamicparam" + "begin" + "process" + "end" + ; TODO: not supported by parser yet, can be used to declare constants + "data" +] @keyword + +; Operators +;---------- +[ + "-and" + "-or" + "-xor" + "-band" + "-bor" + "-bxor" + "+" + "-" + "/" + "\\" + "%" + "*" + ".." + "-not" + "-bnot" + "!" + "=" + "|" + (pre_increment_expression) + (pre_decrement_expression) + (post_increment_expression) + (post_decrement_expression) + (comparison_operator) + (assignement_operator) +] @operator + +; Generic types +;-------------- +(string_literal) @string + +(integer_literal) @number + +(real_literal) @number.float + +(variable) @variable + +((variable) @variable.builtin + (#any-of? @variable.builtin + "$$" "$?" "$^" "$_" "$args" "$ConsoleFileName" "$EnabledExperimentalFeatures" "$Error" "$Event" + "$EventArgs" "$EventSubscriber" "$ExecutionContext" "$false" "$foreach" "$HOME" "$Host" "$input" + "$IsCoreCLR" "$IsLinux" "$IsMacOS" "$IsWindows" "$LASTEXITCODE" "$Matches" "$MyInvocation" + "$NestedPromptLevel" "$null" "$PID" "$PROFILE" "$PSBoundParameters" "$PSCmdlet" "$PSCommandPath" + "$PSCulture" "$PSDebugContext" "$PSEdition" "$PSHOME" "$PSItem" "$PSScriptRoot" "$PSSenderInfo" + "$PSUICulture" "$PSVersionTable" "$PWD" "$Sender" "ShellId" "$StackTrace" "$switch" "$this" + "$true")) + +((variable) @variable.builtin + (#lua-match? @variable.builtin "^\$env:")) + +(comment) @comment @spell + +; Booleans +;--------- +([ + (generic_token) + (command_name) + (variable) + (command) +] @boolean + (#any-of? @boolean "True" "False" "$true" "$false")) + +; switch -regex +;-------------- +(switch_statement + (switch_parameters + (switch_parameter) @_parameter) + (switch_body + (switch_clauses + (switch_clause + (switch_clause_condition + (string_literal) @string.regexp)))) + (#eq? @_parameter "-regex") + (#offset! @string.regexp 0 1 0 -1)) + +; Multiline strings +;------------------ +([ + (verbatim_here_string_characters) + (expandable_here_string_literal) +] @markup.raw.block + (#offset! @markup.raw.block 0 2 0 -2)) + +; Type references and definitions +;-------------------------------- +(type_spec + (type_name) @type) + +(class_statement + (simple_name) @type) + +(type_spec + (type_name) @type.builtin + (#any-of? @type.builtin + "bool" "Boolean" "System.Boolean" "char" "Char" "System.Char" "string" "String" "System.String" + "int" "Int32" "System.Int32" "long" "Int64" "System.Int64" "byte" "Byte" "System.Byte" "float" + "Single" "System.Single" "double" "Double" "System.Double" "decimal" "Decimal" "System.Decimal" + "array" "Array" "System.Array" "regex" "Regex" "System.Text.RegularExpressions.Regex" "ref" + "Management.Automation.PSReference" "System.Management.Automation.PSReference" "scriptblock" + "Management.Automation.ScriptBlock" "System.Management.Automation.ScriptBlock" "datetime" + "DateTime" "System.DateTime" "enum" "Enum" "System.Enum" "IO.DirectoryInfo" + "System.IO.DirectoryInfo" "IO.FileAttributes" "System.IO.FileAttributes" "IO.FileInfo" + "System.IO.FileInfo" "IO.Path" "System.IO.Path" "Text.RegularExpressions.RegexOptions" + "System.Text.RegularExpressions.RegexOptions" "Math" "System.Math" "PSObject" "System.PSObject" + "Object" "System.Object" "ValueType" "System.ValueType" "Hashtable" + "System.Collections.Hashtable" "Collections.Stack" "System.Collections.Generic.Stack" + "Collections.Dictionary" "System.Collections.Dictionary" "Collections.DictionaryEntry" + "System.Collections.DictionaryEntry" "Collections.ArrayList" "System.Collections.ArrayList" + "Management.Automation.SwitchParameter" "System.Management.Automation.SwitchParameter" + "Management.Automation.ActionPreference" "System.Management.Automation.ActionPreference" + "Management.Automation.ConfirmImpact" "System.Management.Automation.ConfirmImpact" + "Management.Automation.CommandInfo" "System.Management.Automation.CommandInfo" + "Management.Automation.ProviderInfo" "System.Management.Automation.ProviderInfo" + "Management.Automation.PSDriveInfo" "System.Management.Automation.PSDriveInfo" + "Management.Automation.PSVariable" "System.Management.Automation.PSVariable" + "Management.Automation.AliasInfo" "System.Management.Automation.AliasInfo" + "Management.Automation.PathInfo" "System.Management.Automation.PathInfo" + "Management.Automation.PathInfoStack" "System.Management.Automation.PathInfoStack" + "Management.Automation.ApplicationInfo" "System.Management.Automation.ApplicationInfo" + "Management.Automation.CmdletInfo" "System.Management.Automation.CmdletInfo" + "Management.Automation.ExternalScriptInfo" "System.Management.Automation.ExternalScriptInfo" + "Management.Automation.FunctionInfo" "System.Management.Automation.FunctionInfo" + "Management.Automation.CommandTypes" "System.Management.CommandTypes" + "Management.Automation.ScopedItemOptions" "System.Management.Automation.ScopedItemOptions" + "Management.Automation.PSTypeName" "System.Management.Automation.PSTypeName" + "Management.Automation.ParameterMetadata" "System.Management.Automation.ParameterMetadata" + "Management.Automation.CommandParameterSetInfo" + "System.Management.Automation.CommandParameterSetInfo" + "Management.Automation.SessionStateEntryVisibility" + "System.Management.Automation.SessionStateEntryVisibility" "Management.Automation.FilterInfo" + "System.Management.Automation.FilterInfo" "Management.Automation.ModuleType" + "System.Management.Automation.ModuleType" "Management.Automation.PSModuleInfo" + "System.Management.Automation.PSModuleInfo" "Management.Automation.PSCustomObject" + "System.Management.Automation.PSCustomObject" "Management.Automation.PSScriptCmdlet" + "System.Management.Automation.PSScriptCmdlet" "Management.Automation.ErrorRecord" + "System.Management.Automation.ErrorRecord" "Management.Automation.PSCredential" + "System.Management.Automation.PSCredential" "Management.Automation.PSMethod" + "System.Management.Automation.PSMethod") + ) + +; Function definitions +;--------------------- +(function_statement + (function_name) @function) + +; Classes, fields and methods definitions +;---------------------------------------- +(class_property_definition + (variable) @variable.member) + +(class_method_definition + (simple_name) @function.method) + +(class_statement + (simple_name) @_class_name + (class_method_definition + (simple_name) @constructor) + (#eq? @constructor @_class_name)) + +(key_expression) @property + +; Functions, methods calls and properties access +;-------------------------- +(command_invokation_operator) @operator + +(invokation_expression + (member_name) @function.call) + +(invokation_expression + (member_name) @constructor + (#eq? @constructor "new")) + +(member_access + (member_name + [ + (simple_name) + (variable) + ] @variable.member)) + +; Commands execution +;------------------- +(command + command_name: (command_name) @function.call) + +(command + command_name: (command_name) @function.builtin + (#any-of? @function.builtin + ; Locations & Paths + "cd" "cd.." "cd\\" "cd~" "help" "Get-Location" "Pop-Location" "Push-Location" "Set-Location" + "Convert-Path" "Join-Path" "Resolve-Path" "Split-Path" "Test-Path" + ; Files & Directories + "Clear-Item" "Clear-ItemProperty" "Copy-Item" "Copy-ItemProperty" "Get-Item" "Get-ItemProperty" + "Get-ItemPropertyValue" "Invoke-Item" "Move-Item" "Move-ItemProperty" "New-Item" + "New-ItemProperty" "Remove-Item" "Remove-ItemProperty" "Rename-Item" "Rename-ItemProperty" + "Set-Item" "Set-ItemProperty" + ; Aliases + "New-Alias" "Get-Alias" "Set-Alias" "Remove-Alias" "Import-Alias" "Export-Alias" + ; Host (prompt) + "prompt" "cls" "echo" "Clear-Host" "Get-Host" "Out-Host" "Read-Host" "Write-Host" "Out-String" + ; Expressions + "Invoke-Expression" + ; Objects + "Compare-Object" "ForEach-Object" "Group-Object" "Measure-Object" "New-Object" "Select-Object" + "Sort-Object" "Tee-Object" "Where-Object" + ; Modules + "Export-ModuleMember" "Find-Module" "Get-Module" "Import-Module" "Install-Module" "New-Module" + "New-ModuleManifest" "Publish-Module" "Remove-Module" "Save-Module" "Test-ModuleManifest" + "Uninstall-Module" "Update-Module" "Update-ModuleManifest")) + +; Parameters and call arguments +;------------------------------ +(command_elements + (generic_token) @variable.parameter) + +(switch_parameter) @variable.parameter.builtin + +(command_parameter) @variable.parameter.builtin + +(script_parameter + (variable) @variable.parameter) + +(class_method_parameter + (variable) @variable.parameter) + +; Document encoding +;------------------ +((program + . + (comment) @keyword.directive @nospell) + (#lua-match? @keyword.directive "Encoding$")) diff --git a/queries/powershell/indents.scm b/queries/powershell/indents.scm new file mode 100644 index 000000000..3be2ed161 --- /dev/null +++ b/queries/powershell/indents.scm @@ -0,0 +1,41 @@ +(function_statement + "}" @indent.branch) @indent.begin + +(param_block + [ + "param" + "(" + ")" + ] @indent.branch) @indent.begin + +(script_block_expression + "}" @indent.branch) @indent.begin + +(statement_block + "}" @indent.branch) @indent.begin + +(switch_body + "}" @indent.branch) @indent.begin + +(array_expression + ")" @indent.branch) @indent.begin + +(hash_literal_expression + "}" @indent.branch) @indent.begin + +(class_statement + "}" @indent.branch) @indent.begin + +(class_method_definition + "}" @indent.branch) @indent.begin + +[ + "}" + ")" +] @indent.end + +[ + (comment) + (string_literal) + (ERROR) +] @indent.auto diff --git a/queries/powershell/injections.scm b/queries/powershell/injections.scm new file mode 100644 index 000000000..677e8b267 --- /dev/null +++ b/queries/powershell/injections.scm @@ -0,0 +1,27 @@ +; comments and block-comments +((comment) @injection.content + (#set! injection.language "comment")) + +; dynamic invocation like & "$env:ChocolateyInstall\bin\choco.exe" +(command + (command_invokation_operator) @_operator + (command_name_expr + (string_literal) @injection.content) + (#eq? @_operator "&") + (#set! injection.language "powershell") + (#offset! @injection.content 0 1 0 -1) + (#set! injection.include-children)) + +; switch -regex +(switch_statement + (switch_parameters + (switch_parameter) @_parameter) + (switch_body + (switch_clauses + (switch_clause + (switch_clause_condition + (string_literal) @injection.content)))) + (#eq? @_parameter "-regex") + (#set! injection.language "regex") + (#offset! @injection.content 0 1 0 -1) + (#set! injection.include-children)) diff --git a/queries/powershell/locals.scm b/queries/powershell/locals.scm new file mode 100644 index 000000000..3a8158d3f --- /dev/null +++ b/queries/powershell/locals.scm @@ -0,0 +1,81 @@ +; Scopes +;------- +(class_statement) @local.scope + +(class_method_definition) @local.scope + +(statement_block) @local.scope + +(function_statement) @local.scope + +; Definitions +;------------ +(class_statement + (simple_name) @local.definition.type + (#set! definition.var.scope "parent")) + +(class_property_definition + (variable) @local.definition.field + (#set! definition.var.scope "parent")) + +(class_method_definition + (simple_name) @local.definition.method + (#set! definition.var.scope "parent")) + +(function_statement + (function_name) @local.definition.function + (#set! definition.var.scope "parent")) + +; function, script block parameters +(parameter_list + (script_parameter + (attribute_list + (attribute + (type_literal + (type_spec) @local.definition.associated))) + (variable) @local.definition.parameter)) + +; variable assignment +(assignment_expression + (left_assignment_expression + (logical_expression + (bitwise_expression + (comparison_expression + (additive_expression + (multiplicative_expression + (format_expression + (range_expression + (array_literal_expression + (unary_expression + (variable) @local.definition.var))))))))))) + +; variable with type assignment +(assignment_expression + (left_assignment_expression + (logical_expression + (bitwise_expression + (comparison_expression + (additive_expression + (multiplicative_expression + (format_expression + (range_expression + (array_literal_expression + (unary_expression + (expression_with_unary_operator + (cast_expression + (type_literal + (type_spec) @local.definition.associated) + (unary_expression + (variable) @local.definition.var)))))))))))))) + +; References +;----------- +(variable) @local.reference + +(command_name) @local.reference + +(invokation_expression + (variable) @_variable + (member_name + (simple_name) @local.reference) + (#eq? @_variable "$this")) |
