Skip to content

Commit 7200bf7

Browse files
dmaclachmarijnh
authored andcommitted
[clike mode] Update default cTypes
Updates the cTypes list to cover newer types (c11) and by default picks up anything ending in '_t' as a type as defined by the POSIX standard (and generally adopted by C compiler types such as int16_t int32_t wchar_t etc).
1 parent ca66e40 commit 7200bf7

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

mode/clike/clike.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,29 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
269269
var cKeywords = "auto if break case register continue return default do sizeof " +
270270
"static else struct switch extern typedef union for goto while enum const " +
271271
"volatile inline restrict asm fortran";
272-
var cTypes = "int long char short double float unsigned signed void size_t ptrdiff_t";
272+
273+
// Do not use this. Use the cTypes function below. This is global just to avoid
274+
// excessive calls when cTypes is being called multiple times during a parse.
275+
var basicCTypes = words("int long char short double float unsigned signed " +
276+
"void bool");
277+
278+
// Do not use this. Use the objCTypes function below. This is global just to avoid
279+
// excessive calls when objCTypes is being called multiple times during a parse.
280+
var basicObjCTypes = words("SEL instancetype id Class Protocol BOOL");
281+
282+
// Returns true if identifier is a "C" type.
283+
// C type is defined as those that are reserved by the compiler (basicTypes),
284+
// and those that end in _t (Reserved by POSIX for types)
285+
// http://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html
286+
function cTypes(identifier) {
287+
return contains(basicCTypes, identifier) || /.+_t/.test(identifier);
288+
}
289+
290+
// Returns true if identifier is a "Objective C" type.
291+
function objCTypes(identifier) {
292+
return cTypes(identifier) || contains(basicObjCTypes, identifier);
293+
}
294+
273295
var cBlockKeywords = "case do else for if switch while struct enum union";
274296
var cDefKeywords = "struct enum union";
275297

@@ -383,8 +405,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
383405
def(["text/x-csrc", "text/x-c", "text/x-chdr"], {
384406
name: "clike",
385407
keywords: words(cKeywords),
386-
types: words(cTypes + " bool float_t double_t intptr_t intmax_t int8_t int16_t " +
387-
"int32_t int64_t uintptr_t uintmax_t uint8_t uint16_t uint32_t uint64_t"),
408+
types: cTypes,
388409
blockKeywords: words(cBlockKeywords),
389410
defKeywords: words(cDefKeywords),
390411
typeFirstDefinitions: true,
@@ -404,7 +425,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
404425
"this using const_cast public throw virtual delete mutable protected " +
405426
"alignas alignof constexpr decltype nullptr noexcept thread_local final " +
406427
"static_assert override"),
407-
types: words(cTypes + " bool wchar_t"),
428+
types: cTypes,
408429
blockKeywords: words(cBlockKeywords +" class try catch finally"),
409430
defKeywords: words(cDefKeywords + " class namespace"),
410431
typeFirstDefinitions: true,
@@ -532,7 +553,6 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
532553
def("text/x-scala", {
533554
name: "clike",
534555
keywords: words(
535-
536556
/* scala */
537557
"abstract case catch class def do else extends final finally for forSome if " +
538558
"implicit import lazy match new null object override package private protected return " +
@@ -730,7 +750,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
730750
keywords: words(cKeywords + " as atomic async call command component components configuration event generic " +
731751
"implementation includes interface module new norace nx_struct nx_union post provides " +
732752
"signal task uses abstract extends"),
733-
types: words(cTypes),
753+
types: cTypes,
734754
blockKeywords: words(cBlockKeywords),
735755
atoms: words("null true false"),
736756
hooks: {"#": cppHook},
@@ -744,7 +764,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
744764
"@interface @implementation @end @protocol @encode @property @synthesize @dynamic @class " +
745765
"@public @package @private @protected @required @optional @try @catch @finally @import " +
746766
"@selector @encode @defs @synchronized @autoreleasepool @compatibility_alias @available"),
747-
types: words(cTypes + " instancetype SEL id BOOL IMP Class"),
767+
types: objCTypes,
748768
builtin: words("FOUNDATION_EXPORT FOUNDATION_EXTERN NS_INLINE NS_FORMAT_FUNCTION NS_RETURNS_RETAINED " +
749769
"NS_ERROR_ENUM NS_RETURNS_NOT_RETAINED NS_RETURNS_INNER_POINTER NS_DESIGNATED_INITIALIZER " +
750770
"NS_ENUM NS_OPTIONS NS_REQUIRES_NIL_TERMINATION NS_ASSUME_NONNULL_BEGIN " +
@@ -753,7 +773,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
753773
defKeywords: words(cDefKeywords + " @interface @implementation @protocol @class"),
754774
dontIndentStatements: /^@.*$/,
755775
typeFirstDefinitions: true,
756-
atoms: words("YES NO NULL Nil nil true false"),
776+
atoms: words("YES NO NULL Nil nil true false nullptr"),
757777
isReservedIdentifier: cIsReservedIdentifier,
758778
hooks: {
759779
"#": cppHook,
@@ -766,7 +786,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
766786
name: "clike",
767787
keywords: words("base break clone continue const default delete enum extends function in class" +
768788
" foreach local resume return this throw typeof yield constructor instanceof static"),
769-
types: words(cTypes),
789+
types: cTypes,
770790
blockKeywords: words("case catch class else for foreach if switch try while"),
771791
defKeywords: words("function local class"),
772792
typeFirstDefinitions: true,

mode/clike/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@
5151
"[builtin __aName];",
5252
"[variable _aName];");
5353

54+
MT("c_types",
55+
"[type int];",
56+
"[type long];",
57+
"[type char];",
58+
"[type short];",
59+
"[type double];",
60+
"[type float];",
61+
"[type unsigned];",
62+
"[type signed];",
63+
"[type void];",
64+
"[type bool];",
65+
"[type foo_t];",
66+
"[variable foo_T];",
67+
"[variable _t];");
68+
5469
var mode_cpp = CodeMirror.getMode({indentUnit: 2}, "text/x-c++src");
5570
function MTCPP(name) { test.mode(name, mode_cpp, Array.prototype.slice.call(arguments, 1)); }
5671

@@ -100,6 +115,18 @@
100115
"}",
101116
"[keyword @end]");
102117

118+
MTOBJC("objc_types",
119+
"[type int];",
120+
"[type foo_t];",
121+
"[variable foo_T];",
122+
"[type id];",
123+
"[type SEL];",
124+
"[type instancetype];",
125+
"[type Class];",
126+
"[type Protocol];",
127+
"[type BOOL];"
128+
);
129+
103130
var mode_scala = CodeMirror.getMode({indentUnit: 2}, "text/x-scala");
104131
function MTSCALA(name) { test.mode("scala_" + name, mode_scala, Array.prototype.slice.call(arguments, 1)); }
105132
MTSCALA("nested_comments",

0 commit comments

Comments
 (0)