Skip to content

Commit aec86c0

Browse files
committed
Sort operators some more
1 parent 96cae36 commit aec86c0

File tree

2 files changed

+49
-34
lines changed

2 files changed

+49
-34
lines changed

PowerShell.sublime-syntax

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ contexts:
363363

364364
in-command:
365365
- meta_content_scope: meta.function-call.arguments.powershell
366-
- include: command-ending-sequences
366+
- include: pop-end-of-command
367367
- include: redirection
368368
- include: cli-parameters
369369
- include: expressions-without-commands
@@ -377,22 +377,28 @@ contexts:
377377
- include: escape-sequences
378378
- include: string-wildcards
379379

380-
command-ending-sequences:
380+
pop-end-of-command:
381381
- include: line-continuations
382382
- include: pop-eol
383383
- match: (?=[)}])
384384
pop: 1
385+
- include: pop-terminator
386+
- include: pop-pipe
387+
388+
pop-terminator:
385389
- match: ';'
386390
scope: punctuation.terminator.statement.powershell
387391
pop: 1
392+
393+
pop-pipe:
388394
- match: (\|)\s*\n
389395
captures:
390-
1: keyword.operator.logical.pipe.powershell
396+
1: keyword.operator.assignment.pipe.powershell
391397
push:
392398
- match: ^
393399
pop: 2
394400
- match: \|
395-
scope: keyword.operator.logical.pipe.powershell
401+
scope: keyword.operator.assignment.pipe.powershell
396402
pop: 1
397403

398404
cli-parameters:
@@ -841,7 +847,22 @@ contexts:
841847
###[ OPERATORS ]###############################################################
842848

843849
operators:
844-
# Word operators
850+
- include: operator-words
851+
- include: operator-symbols
852+
853+
operator-words:
854+
# RegExp context
855+
- match: \B(-)(?i:[ic]?(?:not)?(?:match)){{kebab_break}}
856+
scope: keyword.operator.logical.powershell
857+
captures:
858+
1: punctuation.definition.keyword.powershell
859+
push: expect-regex-string
860+
- match: \B(-)(?i:replace){{kebab_break}}
861+
scope: keyword.operator.string.powershell
862+
captures:
863+
1: punctuation.definition.keyword.powershell
864+
push: expect-regex-string
865+
845866
- match: \B(-)(?i:as){{kebab_break}}
846867
scope: keyword.operator.cast.powershell
847868
captures:
@@ -850,20 +871,10 @@ contexts:
850871
scope: keyword.operator.comparison.powershell
851872
captures:
852873
1: punctuation.definition.keyword.powershell
853-
- match: \B(-)(?i:[ic]?(?:not)?(?:match)){{kebab_break}}
854-
scope: keyword.operator.logical.powershell
855-
captures:
856-
1: punctuation.definition.keyword.powershell
857-
push: expect-regex-string
858874
- match: \B(-)(?i:[ic]?(?:not)?(?:like|contains|in)){{kebab_break}}
859875
scope: keyword.operator.logical.powershell
860876
captures:
861877
1: punctuation.definition.keyword.powershell
862-
- match: \B(-)(?i:replace){{kebab_break}}
863-
scope: keyword.operator.string.powershell
864-
captures:
865-
1: punctuation.definition.keyword.powershell
866-
push: expect-regex-string
867878
- match: \B(-)(?i:join|split){{kebab_break}}
868879
scope: keyword.operator.string.powershell
869880
captures:
@@ -885,31 +896,38 @@ contexts:
885896
captures:
886897
1: punctuation.definition.keyword.powershell
887898

888-
# Symbol operators
889-
- match: \!
890-
scope: keyword.operator.logical.powershell
899+
operator-symbols:
900+
# Assignment context
891901
- include: assignment-normal
892902
- match: (?:[+/*%-]|\?\?)=
893903
scope: keyword.operator.assignment.augmented.powershell
894904
push: in-assignment
905+
# Assignment same context
906+
- match: \|
907+
scope: keyword.operator.assignment.pipe.powershell
895908
- match: (?:\+\+|--)(?![ \t]*\d|[[:alpha:]])
896909
scope: keyword.operator.assignment.powershell
897-
- match: '[+-](?=\.?\d)' # This is sort of heuristic
910+
911+
# Heuristically guess the context with lookaheads
912+
# Mostly to avoid matching CLI parameters
913+
- match: '[+-](?=\.?\d)'
898914
scope: keyword.operator.unary.powershell
899-
# Be careful about matching arithmetic with tightly following words
900915
- match: '[/+](?![[:alpha:]])'
901916
scope: keyword.operator.arithmetic.powershell
902917
- match: -(?![[:alpha:]-])
903918
scope: keyword.operator.arithmetic.powershell
904919
- match: \*(?!\.?[[:alpha:]])
905920
scope: keyword.operator.arithmetic.powershell
921+
- match: \.\.(?=\-?\d|\(|\$)
922+
scope: keyword.operator.range.powershell
906923
# Exclude foreach shortcut
907924
- match: '%(?!\s*\{)'
908925
scope: keyword.operator.arithmetic.powershell
926+
927+
- match: \!
928+
scope: keyword.operator.logical.powershell
909929
- match: \|\||&&
910930
scope: keyword.operator.logical.powershell
911-
- match: \|
912-
scope: keyword.operator.logical.pipe.powershell
913931
- match: \?\?
914932
scope: keyword.operator.null-coalescing.powershell
915933
- match: '[?:]'
@@ -919,17 +937,14 @@ contexts:
919937
- include: comma-separators
920938
- match: '&'
921939
scope: keyword.operator.background.powershell
922-
- match: \.\.(?=\-?\d|\(|\$)
923-
# This is very imprecise. Is there a syntax for 'must come after...'?
924-
scope: keyword.operator.range.powershell
925940

926941
assignment-normal:
927942
- match: =
928943
scope: keyword.operator.assignment.powershell
929944
push: in-assignment
930945

931946
in-assignment:
932-
- include: command-ending-sequences
947+
- include: pop-end-of-command
933948
# INFO: Someday maybe we can nix the workflow stuff
934949
- include: workflow-execution-contexts
935950
- include: expressions

Tests/syntax_test_PowerShell.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ $a3[1..2]
880880
# ^ meta.number.integer.decimal.powershell constant.numeric.value.powershell
881881
# ^^ keyword.operator.range.powershell
882882
# ^^ meta.number.integer.decimal.powershell constant.numeric.value.powershell
883-
# ^ keyword.operator.logical.pipe.powershell
883+
# ^ keyword.operator.assignment.pipe.powershell
884884
# ^^^^^^^^^^^^^^^^ meta.function-call.powershell support.function.powershell
885885
# @@@@@@@@@@@@@@@@ reference
886886
# ^ punctuation.section.group.end.powershell
@@ -899,7 +899,7 @@ $a3[1..2]
899899
# ^^^ variable.parameter.option.powershell
900900
# ^ punctuation.definition.parameter.powershell
901901
# ^^ meta.number.integer.decimal.powershell constant.numeric.value.powershell
902-
# ^ keyword.operator.logical.pipe.powershell
902+
# ^ keyword.operator.assignment.pipe.powershell
903903
# ^^^^^^^^^^^^^^^^ meta.function-call.powershell support.function.powershell
904904
# @@@@@@@@@@@@@@@@ reference
905905
# ^^^^^^^^ meta.function-call.arguments.powershell
@@ -926,7 +926,7 @@ $a3[1..2]
926926
#^^^^^^^^^ meta.function-call.arguments.powershell
927927
#^^^^^^^^ meta.block.powershell
928928
# ^ punctuation.section.braces.end.powershell
929-
# ^ keyword.operator.logical.pipe.powershell
929+
# ^ keyword.operator.assignment.pipe.powershell
930930
# ^^^^^^^^^^^^^^^^ meta.function-call.powershell support.function.powershell
931931
# @@@@@@@@@@@@@@@@ reference
932932

@@ -952,7 +952,7 @@ $a3[1..2]
952952
# ^ punctuation.definition.parameter.powershell
953953
# ^^^^^^ variable.other.readwrite.powershell
954954
# ^ punctuation.definition.variable.powershell
955-
# ^ keyword.operator.logical.pipe.powershell
955+
# ^ keyword.operator.assignment.pipe.powershell
956956
# ^^^^^^^^^^^^^^^^ meta.function-call.powershell support.function.powershell
957957
# @@@@@@@@@@@@@@@@ reference
958958
# ^^^^^^^^^ meta.function-call.arguments.powershell
@@ -963,7 +963,7 @@ $a3[1..2]
963963
# ^^^^^^^^^^^^^ meta.function-call.powershell support.function.powershell
964964
# @@@@@@@@@@@@@ reference
965965
# ^^ meta.function-call.arguments.powershell
966-
# ^ keyword.operator.logical.pipe.powershell
966+
# ^ keyword.operator.assignment.pipe.powershell
967967
Select Name,Length
968968
# ^^^^^^ meta.function-call.powershell variable.function.powershell
969969
# @@@@@@ reference
@@ -2115,7 +2115,7 @@ function out-file {}
21152115
$ScriptBlock | Out-File $file -Force
21162116
# <- punctuation.definition.variable
21172117
# @@@@@@@@ reference
2118-
# ^ keyword.operator.logical.pipe
2118+
# ^ keyword.operator.assignment.pipe
21192119
# ^ punctuation.definition.variable
21202120
# ^ punctuation.definition.parameter
21212121
# ^^^^^^ variable.parameter.option
@@ -2128,7 +2128,7 @@ function Out-WithYou {}
21282128
#@@@@@@@@@ reference
21292129
# ^^^^^^^^^^^ support.function
21302130
# @@@@@@@@@@@ reference
2131-
# ^ keyword.operator.logical.pipe
2131+
# ^ keyword.operator.assignment.pipe
21322132
# ^ keyword.operator.redirection
21332133
# ^ punctuation.definition.variable
21342134
# ^ constant.language
@@ -2142,7 +2142,7 @@ function get-number {}
21422142
# @@@@@@@@@@ reference
21432143
# @@@@@@@@@@@@@@ reference
21442144
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.powershell.embedded
2145-
# ^ keyword.operator.logical.pipe
2145+
# ^ keyword.operator.assignment.pipe
21462146
# ^ meta.group.complex.subexpression punctuation.section.group.begin
21472147
# ^^^^^^ storage.type
21482148
# ^^ punctuation.accessor.double-colon

0 commit comments

Comments
 (0)