Skip to content

Commit

Permalink
[C] Make function parameter names optional (#4093)
Browse files Browse the repository at this point in the history
Function parameter names are now optional in C and C++. Previously
the syntax highlighting rules would assume that the last identifier
found in a parameter was the parameter name but this was making it
impossible to properly highlight parameters without a parameter name.

The only solution I could find was to include the specific types and
modifiers before the more general match for the parameter name. With
this, the types and modifiers are no longer mistaken for a parameter
name.
  • Loading branch information
braewoods authored Nov 17, 2024
1 parent f794bce commit cccff30
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 8 deletions.
4 changes: 2 additions & 2 deletions C++/C++.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -1223,8 +1223,8 @@ contexts:
- match : \)
scope: punctuation.section.group.end.c++
set: function-definition-continue
- match: '\bvoid\b'
scope: storage.type.c++
- include: modifiers
- include: types
- match: '{{identifier}}(?=\s*(\[|,|\)|=))'
scope: variable.parameter.c++
- match: '='
Expand Down
4 changes: 2 additions & 2 deletions C++/C.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,8 @@ contexts:
- match : \)
scope: punctuation.section.group.end.c
set: function-definition-continue
- match: '\bvoid\b'
scope: storage.type.c
- include: modifiers
- include: types
- match: '{{identifier}}(?=\s*(\[|,|\)))'
scope: variable.parameter.c
- include: expressions
Expand Down
17 changes: 17 additions & 0 deletions C++/syntax_test_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,23 @@ struct UI_MenuBoxData
// Test preprocessor branching and C blocks
/////////////////////////////////////////////

int bar(int, int const *, int const * const);
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.parameters meta.group */
/* ^ punctuation.section.group.begin */
/* ^ punctuation.section.group.end */
/* ^ punctuation.terminator */
/* ^^^ storage.type */
/* ^ punctuation.separator */
/* ^^^ storage.type */
/* ^^^^^ storage.modifier */
/* ^ keyword.operator */
/* ^ punctuation.separator */
/* ^^^ storage.type */
/* ^^^^^ storage.modifier */
/* ^ keyword.operator */
/* ^^^^^ storage.modifier */

int foo(int val, float val2[])
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function */
/* ^^^^^^^^^^^^^^^^^^^^^^^ meta.function.parameters meta.group */
Expand Down
17 changes: 17 additions & 0 deletions C++/syntax_test_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2547,6 +2547,23 @@ MyEnum MACRO1
// Test preprocessor branching and C blocks
/////////////////////////////////////////////

int bar(int, int const *, int const * const);
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.parameters meta.group */
/* ^ punctuation.section.group.begin */
/* ^ punctuation.section.group.end */
/* ^ punctuation.terminator */
/* ^^^ storage.type */
/* ^ punctuation.separator */
/* ^^^ storage.type */
/* ^^^^^ storage.modifier */
/* ^ keyword.operator */
/* ^ punctuation.separator */
/* ^^^ storage.type */
/* ^^^^^ storage.modifier */
/* ^ keyword.operator */
/* ^^^^^ storage.modifier */

int foo(int val, float val2[], bool val3 = false)
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.parameters meta.group */
Expand Down
4 changes: 2 additions & 2 deletions Objective-C/Objective-C++.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -1113,8 +1113,8 @@ contexts:
- match : \)
scope: punctuation.section.group.end.objc++
set: function-definition-continue
- match: '\bvoid\b'
scope: storage.type.objc++
- include: modifiers
- include: types
- match: '{{identifier}}(?=\s*(\[|,|\)|=))'
scope: variable.parameter.objc++
- match: '='
Expand Down
4 changes: 2 additions & 2 deletions Objective-C/Objective-C.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,8 @@ contexts:
- match : \)
scope: punctuation.section.group.end.objc
set: function-definition-continue
- match: '\bvoid\b'
scope: storage.type.objc
- include: modifiers
- include: types
- match: '{{identifier}}(?=\s*(\[|,|\)))'
scope: variable.parameter.objc
- include: expressions
Expand Down
17 changes: 17 additions & 0 deletions Objective-C/syntax_test_objc++.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2501,6 +2501,23 @@ T operator()(int a) {
// Test preprocessor branching and C blocks
/////////////////////////////////////////////

int bar(int, int const *, int const * const);
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.parameters meta.group */
/* ^ punctuation.section.group.begin */
/* ^ punctuation.section.group.end */
/* ^ punctuation.terminator */
/* ^^^ storage.type */
/* ^ punctuation.separator */
/* ^^^ storage.type */
/* ^^^^^ storage.modifier */
/* ^ keyword.operator */
/* ^ punctuation.separator */
/* ^^^ storage.type */
/* ^^^^^ storage.modifier */
/* ^ keyword.operator */
/* ^^^^^ storage.modifier */

int foo(int val, float val2[], bool val3 = false)
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.parameters meta.group */
Expand Down
17 changes: 17 additions & 0 deletions Objective-C/syntax_test_objc.m
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,23 @@ int main(void)
// Test preprocessor branching and C blocks
/////////////////////////////////////////////
int bar(int, int const *, int const * const);
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.parameters meta.group */
/* ^ punctuation.section.group.begin */
/* ^ punctuation.section.group.end */
/* ^ punctuation.terminator */
/* ^^^ storage.type */
/* ^ punctuation.separator */
/* ^^^ storage.type */
/* ^^^^^ storage.modifier */
/* ^ keyword.operator */
/* ^ punctuation.separator */
/* ^^^ storage.type */
/* ^^^^^ storage.modifier */
/* ^ keyword.operator */
/* ^^^^^ storage.modifier */
int foo(int val, float val2[])
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function */
/* ^^^^^^^^^^^^^^^^^^^^^^^ meta.function.parameters meta.group */
Expand Down

0 comments on commit cccff30

Please sign in to comment.