Skip to content

Commit

Permalink
[C/ObjC] Fix: be more lenient about macro identifiers with enum retur…
Browse files Browse the repository at this point in the history
…n types (sublimehq#1718)

* [C/ObjC] Fix: be more lenient about macro identifiers with enum return types
* [C/Objective-C] Fix handling of __declspec() after struct/enum/union
  • Loading branch information
rwols authored and wbond committed Jul 29, 2019
1 parent 81f6c7e commit b94f72a
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 8 deletions.
43 changes: 39 additions & 4 deletions C++/C.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,11 @@ contexts:
- include: comments
- match: \*
scope: keyword.operator.c
# If a struct/union/enum followed by a name that is not a macro or declspec
# then this is likely a return type of a function. This is uncommon.
- match: |-
(?x:
({{before_tag}})
\s+
(?=
(?![[:upper:][:digit:]_]+\b|__declspec)
{{identifier}}
(\s+{{identifier}}(?!\s*[{=;])|\s*\*+)
)
Expand Down Expand Up @@ -468,12 +465,24 @@ contexts:
data-structures-struct-definition:
- meta_scope: meta.struct.c
- include: data-structures-definition-common-begin
- include: data-structures-definition-common-macro
- match: '{{identifier}}(?=\s*;)'
scope: entity.name.struct.forward-decl.c
- match: '{{identifier}}'
scope: entity.name.struct.c
set: data-structures-struct-definition-after-name
- include: data-structures-struct-definition-block-start
- match: '(?=;)'
pop: true

data-structures-struct-definition-after-name:
- meta_scope: meta.struct.c
- include: data-structures-definition-common-begin
- match: '(?=;)'
pop: true
- include: data-structures-struct-definition-block-start

data-structures-struct-definition-block-start:
- match: '\{'
scope: meta.block.c punctuation.section.block.begin.c
set:
Expand All @@ -486,12 +495,24 @@ contexts:
data-structures-enum-definition:
- meta_scope: meta.enum.c
- include: data-structures-definition-common-begin
- include: data-structures-definition-common-macro
- match: '{{identifier}}(?=\s*;)'
scope: entity.name.enum.forward-decl.c
- match: '{{identifier}}'
scope: entity.name.enum.c
set: data-structures-enum-definition-after-name
- include: data-structures-enum-definition-block-start
- match: '(?=;)'
pop: true

data-structures-enum-definition-after-name:
- meta_scope: meta.enum.c
- include: data-structures-definition-common-begin
- match: '(?=;)'
pop: true
- include: data-structures-enum-definition-block-start

data-structures-enum-definition-block-start:
- match: '\{'
scope: meta.block.c punctuation.section.block.begin.c
set:
Expand All @@ -505,12 +526,24 @@ contexts:
data-structures-union-definition:
- meta_scope: meta.union.c
- include: data-structures-definition-common-begin
- include: data-structures-definition-common-macro
- match: '{{identifier}}(?=\s*;)'
scope: entity.name.union.forward-decl.c
- match: '{{identifier}}'
scope: entity.name.union.c
set: data-structures-union-definition-after-name
- include: data-structures-union-definition-block-start
- match: '(?=;)'
pop: true

data-structures-union-definition-after-name:
- meta_scope: meta.union.c
- include: data-structures-definition-common-begin
- match: '(?=;)'
pop: true
- include: data-structures-union-definition-block-start

data-structures-union-definition-block-start:
- match: '\{'
scope: meta.block.c punctuation.section.block.begin.c
set:
Expand All @@ -526,8 +559,10 @@ contexts:
pop: true
- include: modifiers-parens
- include: modifiers

data-structures-definition-common-macro:
# Handle macros so they aren't matched as the class name
- match: '\b[[:upper:][:digit:]_]+\b'
- match: '\b[[:upper:][:digit:]_]+\b(?!\s*($|\{))'

data-structures-definition-common-end:
- match: '(?=;)'
Expand Down
34 changes: 34 additions & 0 deletions C++/syntax_test_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@ int main(){
/* ^^ keyword.operator.arithmetic */
}

enum Foo { kFoo, kBar };
#define FOO Foo
enum FOO do_the_foo(void);
/* ^ entity.name.function */
/* ^ storage.type */

#define APIC_CAPABILITY TheEnum
enum TheEnum { kFoo, kBar };
static enum APIC_CAPABILITY apic_capabilities(void) { return kFoo; };
/* ^ entity.name.function */
/* ^ storage.type */

struct __declspec(dllimport) X {};
/* ^ storage.modifier */
/* ^ entity.name.struct */

struct __declspec(dllimport) baz X {};
/* ^ storage.modifier */
/* ^ entity.name.struct */

struct foo {
/* ^ entity.name.struct */
union {
/* ^ storage.type */
struct {
/* ^ storage.type */
int a;
/* ^ storage.type */
int b;
/* ^ storage.type */
}
}
}

#define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS /* comment block */ * sizeof(struct ptp_extts_event)) // comment line
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.preprocessor.macro */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.group */
Expand Down
43 changes: 39 additions & 4 deletions Objective-C/Objective-C.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,11 @@ contexts:
- include: comments
- match: \*
scope: keyword.operator.objc
# If a struct/union/enum followed by a name that is not a macro or declspec
# then this is likely a return type of a function. This is uncommon.
- match: |-
(?x:
({{before_tag}})
\s+
(?=
(?![[:upper:][:digit:]_]+\b|__declspec)
{{identifier}}
(\s+{{identifier}}(?!\s*[{=;])|\s*\*+)
)
Expand Down Expand Up @@ -668,12 +665,24 @@ contexts:
data-structures-struct-definition:
- meta_scope: meta.struct.objc
- include: data-structures-definition-common-begin
- include: data-structures-definition-common-macro
- match: '{{identifier}}(?=\s*;)'
scope: entity.name.struct.forward-decl.objc
- match: '{{identifier}}'
scope: entity.name.struct.objc
set: data-structures-struct-definition-after-name
- include: data-structures-struct-definition-block-start
- match: '(?=;)'
pop: true

data-structures-struct-definition-after-name:
- meta_scope: meta.struct.objc
- include: data-structures-definition-common-begin
- match: '(?=;)'
pop: true
- include: data-structures-struct-definition-block-start

data-structures-struct-definition-block-start:
- match: '\{'
scope: meta.block.objc punctuation.section.block.begin.objc
set:
Expand All @@ -686,12 +695,24 @@ contexts:
data-structures-enum-definition:
- meta_scope: meta.enum.objc
- include: data-structures-definition-common-begin
- include: data-structures-definition-common-macro
- match: '{{identifier}}(?=\s*;)'
scope: entity.name.enum.forward-decl.objc
- match: '{{identifier}}'
scope: entity.name.enum.objc
set: data-structures-enum-definition-after-name
- include: data-structures-enum-definition-block-start
- match: '(?=;)'
pop: true

data-structures-enum-definition-after-name:
- meta_scope: meta.enum.objc
- include: data-structures-definition-common-begin
- match: '(?=;)'
pop: true
- include: data-structures-enum-definition-block-start

data-structures-enum-definition-block-start:
- match: '\{'
scope: meta.block.objc punctuation.section.block.begin.objc
set:
Expand All @@ -705,12 +726,24 @@ contexts:
data-structures-union-definition:
- meta_scope: meta.union.objc
- include: data-structures-definition-common-begin
- include: data-structures-definition-common-macro
- match: '{{identifier}}(?=\s*;)'
scope: entity.name.union.forward-decl.objc
- match: '{{identifier}}'
scope: entity.name.union.objc
set: data-structures-union-definition-after-name
- include: data-structures-union-definition-block-start
- match: '(?=;)'
pop: true

data-structures-union-definition-after-name:
- meta_scope: meta.union.objc
- include: data-structures-definition-common-begin
- match: '(?=;)'
pop: true
- include: data-structures-union-definition-block-start

data-structures-union-definition-block-start:
- match: '\{'
scope: meta.block.objc punctuation.section.block.begin.objc
set:
Expand All @@ -726,8 +759,10 @@ contexts:
pop: true
- include: modifiers-parens
- include: modifiers

data-structures-definition-common-macro:
# Handle macros so they aren't matched as the class name
- match: '\b[[:upper:][:digit:]_]+\b'
- match: '\b[[:upper:][:digit:]_]+\b(?!\s*($|\{))'

data-structures-definition-common-end:
- match: '(?=;)'
Expand Down
34 changes: 34 additions & 0 deletions Objective-C/syntax_test_objc.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@ int main(){
/* ^^ keyword.operator.arithmetic */
}

enum Foo { kFoo, kBar };
#define FOO Foo
enum FOO do_the_foo(void);
/* ^ entity.name.function */
/* ^ storage.type */

#define APIC_CAPABILITY TheEnum
enum TheEnum { kFoo, kBar };
static enum APIC_CAPABILITY apic_capabilities(void) { return kFoo; };
/* ^ entity.name.function */
/* ^ storage.type */

struct __declspec(dllimport) X {};
/* ^ storage.modifier */
/* ^ entity.name.struct */

struct __declspec(dllimport) baz X {};
/* ^ storage.modifier */
/* ^ entity.name.struct */

struct foo {
/* ^ entity.name.struct */
union {
/* ^ storage.type */
struct {
/* ^ storage.type */
int a;
/* ^ storage.type */
int b;
/* ^ storage.type */
}
}
}

#define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS /* comment block */ * sizeof(struct ptp_extts_event)) // comment line
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.preprocessor.macro */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.group */
Expand Down

0 comments on commit b94f72a

Please sign in to comment.