Skip to content

Commit 0874ab1

Browse files
dmaclachadrianheine
authored andcommitted
[clike mode] Improve Objective C coloring and editing
Also cleans up defines and block keywords for C/C++.
1 parent facc5d9 commit 0874ab1

2 files changed

Lines changed: 46 additions & 17 deletions

File tree

mode/clike/clike.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
270270
"static else struct switch extern typedef union for goto while enum const " +
271271
"volatile inline restrict asm fortran";
272272
var cTypes = "int long char short double float unsigned signed void size_t ptrdiff_t";
273+
var cBlockKeywords = "case do else for if switch while struct enum union";
274+
var cDefKeywords = "struct enum union";
273275

274276
function cppHook(stream, state) {
275277
if (!state.startOfLine) return false
@@ -383,8 +385,8 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
383385
keywords: words(cKeywords),
384386
types: words(cTypes + " bool float_t double_t intptr_t intmax_t int8_t int16_t " +
385387
"int32_t int64_t uintptr_t uintmax_t uint8_t uint16_t uint32_t uint64_t"),
386-
blockKeywords: words("case do else for if switch while struct"),
387-
defKeywords: words("struct"),
388+
blockKeywords: words(cBlockKeywords),
389+
defKeywords: words(cDefKeywords),
388390
typeFirstDefinitions: true,
389391
atoms: words("NULL true false"),
390392
isReservedIdentifier: cIsReservedIdentifier,
@@ -403,8 +405,8 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
403405
"alignas alignof constexpr decltype nullptr noexcept thread_local final " +
404406
"static_assert override"),
405407
types: words(cTypes + " bool wchar_t"),
406-
blockKeywords: words("catch class do else finally for if struct switch try while"),
407-
defKeywords: words("class namespace struct enum union"),
408+
blockKeywords: words(cBlockKeywords +" class try catch finally"),
409+
defKeywords: words(cDefKeywords + " class namespace"),
408410
typeFirstDefinitions: true,
409411
atoms: words("true false NULL"),
410412
dontIndentStatements: /^template$/,
@@ -729,31 +731,35 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
729731
"implementation includes interface module new norace nx_struct nx_union post provides " +
730732
"signal task uses abstract extends"),
731733
types: words(cTypes),
732-
blockKeywords: words("case do else for if switch while struct"),
734+
blockKeywords: words(cBlockKeywords),
733735
atoms: words("null true false"),
734736
hooks: {"#": cppHook},
735737
modeProps: {fold: ["brace", "include"]}
736738
});
737739

738740
def("text/x-objectivec", {
739741
name: "clike",
740-
keywords: words(cKeywords + " bycopy byref in inout oneway out self " +
741-
"super atomic nonatomic retain copy readwrite readonly " +
742-
"strong weak assign typeof nullable nonnull instancetype"),
743-
types: words(cTypes),
742+
keywords: words(cKeywords + " bycopy byref in inout oneway out self super atomic nonatomic retain copy " +
743+
"readwrite readonly strong weak assign typeof nullable nonnull null_resettable _cmd " +
744+
"@interface @implementation @end @protocol @encode @property @synthesize @dynamic @class " +
745+
"@public @package @private @protected @required @optional @try @catch @finally @import " +
746+
"@selector @encode @defs @synchronized @autoreleasepool @compatibility_alias @available"),
747+
types: words(cTypes + " instancetype SEL id BOOL IMP Class"),
748+
builtin: words("FOUNDATION_EXPORT FOUNDATION_EXTERN NS_INLINE NS_FORMAT_FUNCTION NS_RETURNS_RETAINED " +
749+
"NS_ERROR_ENUM NS_RETURNS_NOT_RETAINED NS_RETURNS_INNER_POINTER NS_DESIGNATED_INITIALIZER " +
750+
"NS_ENUM NS_OPTIONS NS_REQUIRES_NIL_TERMINATION NS_ASSUME_NONNULL_BEGIN " +
751+
"NS_ASSUME_NONNULL_END NS_SWIFT_NAME NS_REFINED_FOR_SWIFT"),
752+
blockKeywords: words(cBlockKeywords + " @synthesize @try @catch @finally @autoreleasepool @synchronized"),
753+
defKeywords: words(cDefKeywords + " @interface @implementation @protocol @class"),
754+
dontIndentStatements: /^@.*$/,
755+
typeFirstDefinitions: true,
744756
atoms: words("YES NO NULL Nil nil true false"),
745757
isReservedIdentifier: cIsReservedIdentifier,
746758
hooks: {
747-
"@": function(stream) {
748-
stream.eatWhile(/[\w\$]/);
749-
return "keyword";
750-
},
751759
"#": cppHook,
752-
indent: function(_state, ctx, textAfter) {
753-
if (ctx.type == "statement" && /^@\w/.test(textAfter)) return ctx.indented
754-
}
760+
"*": pointerHook,
755761
},
756-
modeProps: {fold: "brace"}
762+
modeProps: {fold: ["brace", "include"]}
757763
});
758764

759765
def("text/x-squirrel", {

mode/clike/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
MT("def",
2424
"[type void] [def foo]() {}",
2525
"[keyword struct] [def bar]{}",
26+
"[keyword enum] [def zot]{}",
27+
"[keyword union] [def ugh]{}",
2628
"[type int] [type *][def baz]() {}");
2729

2830
MT("def_new_line",
@@ -77,6 +79,27 @@
7779
"[builtin __aName];",
7880
"[variable _aName];");
7981

82+
MTOBJC("objc_interface",
83+
"[keyword @interface] [def foo] {",
84+
" [type int] [variable bar];",
85+
"}",
86+
"[keyword @property] ([keyword atomic], [keyword nullable]) [variable NSString][operator *] [variable a];",
87+
"[keyword @property] ([keyword nonatomic], [keyword assign]) [type int] [variable b];",
88+
"[operator -]([type instancetype])[variable initWithFoo]:([type int])[variable a] " +
89+
"[builtin NS_DESIGNATED_INITIALIZER];",
90+
"[keyword @end]");
91+
92+
MTOBJC("objc_implementation",
93+
"[keyword @implementation] [def foo] {",
94+
" [type int] [variable bar];",
95+
"}",
96+
"[keyword @property] ([keyword readwrite]) [type SEL] [variable a];",
97+
"[operator -]([type instancetype])[variable initWithFoo]:([type int])[variable a] {",
98+
" [keyword if](([keyword self] [operator =] [[[keyword super] [variable init] ]])) {}",
99+
" [keyword return] [keyword self];",
100+
"}",
101+
"[keyword @end]");
102+
80103
var mode_scala = CodeMirror.getMode({indentUnit: 2}, "text/x-scala");
81104
function MTSCALA(name) { test.mode("scala_" + name, mode_scala, Array.prototype.slice.call(arguments, 1)); }
82105
MTSCALA("nested_comments",

0 commit comments

Comments
 (0)