Skip to content

Commit

Permalink
enh(parser) beginKeyword no longer bestows double relevance (#2953)
Browse files Browse the repository at this point in the history
This changes the default relevance of a mode containing beginKeywords
to 0 forcing it to only gain relevance from keyword matches alone.

Previously something like `beginKeywords: "def"` would double score
`def` because of the implied `keywords: 'def'` that is ALSO present in
additional to the mode itself.

This of course revealed a few auto-detect balance issues (because some
grammars had been benefiting from this extra boost) which this PR
also resolves.
  • Loading branch information
joshgoebel authored Jan 19, 2021
1 parent 871c00b commit 89f4e84
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 115 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Grammar changes:

Parser:

- `beginKeyword` no longer bestows double relevance (#2953) [Josh Goebel][]
- allow `keywords` to be an array of strings [Josh Goebel][]
- add `modes.MATCH_NOTHING_RE` that will never match
- This can be used with `end` to hold a mode open (it must then be ended with
Expand Down
4 changes: 3 additions & 1 deletion src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,9 @@ const HLJS = function(hljs) {
result = emitter.toHTML();

return {
relevance: relevance,
// avoid possible breakage with v10 clients expecting
// this to always be an integer
relevance: Math.floor(relevance),
value: result,
language: languageName,
illegal: false,
Expand Down
16 changes: 9 additions & 7 deletions src/languages/clojure-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ Category: lisp
export default function(hljs) {
return {
name: 'Clojure REPL',
contains: [{
className: 'meta',
begin: /^([\w.-]+|\s*#_)?=>/,
starts: {
end: /$/,
subLanguage: 'clojure'
contains: [
{
className: 'meta',
begin: /^([\w.-]+|\s*#_)?=>/,
starts: {
end: /$/,
subLanguage: 'clojure'
}
}
}]
]
};
}
85 changes: 58 additions & 27 deletions src/languages/clojure.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Category: lisp

/** @type LanguageFn */
export default function(hljs) {
var SYMBOLSTART = 'a-zA-Z_\\-!.?+*=<>&#\'';
var SYMBOL_RE = '[' + SYMBOLSTART + '][' + SYMBOLSTART + '0-9/;:]*';
var globals = 'def defonce defprotocol defstruct defmulti defmethod defn- defn defmacro deftype defrecord';
var keywords = {
const SYMBOLSTART = 'a-zA-Z_\\-!.?+*=<>&#\'';
const SYMBOL_RE = '[' + SYMBOLSTART + '][' + SYMBOLSTART + '0-9/;:]*';
const globals = 'def defonce defprotocol defstruct defmulti defmethod defn- defn defmacro deftype defrecord';
const keywords = {
$pattern: SYMBOL_RE,
'builtin-name':
// Clojure keywords
Expand Down Expand Up @@ -45,57 +45,73 @@ export default function(hljs) {
'lazy-seq spread list* str find-keyword keyword symbol gensym force rationalize'
};

var SIMPLE_NUMBER_RE = '[-+]?\\d+(\\.\\d+)?';
const SIMPLE_NUMBER_RE = '[-+]?\\d+(\\.\\d+)?';

var SYMBOL = {
const SYMBOL = {
begin: SYMBOL_RE,
relevance: 0
};
var NUMBER = {
className: 'number', begin: SIMPLE_NUMBER_RE,
const NUMBER = {
className: 'number',
begin: SIMPLE_NUMBER_RE,
relevance: 0
};
var STRING = hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null});
var COMMENT = hljs.COMMENT(
const STRING = hljs.inherit(hljs.QUOTE_STRING_MODE, {
illegal: null
});
const COMMENT = hljs.COMMENT(
';',
'$',
{
relevance: 0
}
);
var LITERAL = {
const LITERAL = {
className: 'literal',
begin: /\b(true|false|nil)\b/
};
var COLLECTION = {
begin: '[\\[\\{]', end: '[\\]\\}]'
const COLLECTION = {
begin: '[\\[\\{]',
end: '[\\]\\}]'
};
var HINT = {
const HINT = {
className: 'comment',
begin: '\\^' + SYMBOL_RE
};
var HINT_COL = hljs.COMMENT('\\^\\{', '\\}');
var KEY = {
const HINT_COL = hljs.COMMENT('\\^\\{', '\\}');
const KEY = {
className: 'symbol',
begin: '[:]{1,2}' + SYMBOL_RE
};
var LIST = {
begin: '\\(', end: '\\)'
const LIST = {
begin: '\\(',
end: '\\)'
};
var BODY = {
const BODY = {
endsWithParent: true,
relevance: 0
};
var NAME = {
const NAME = {
keywords: keywords,
className: 'name',
begin: SYMBOL_RE,
relevance: 0,
starts: BODY
};
var DEFAULT_CONTAINS = [LIST, STRING, HINT, HINT_COL, COMMENT, KEY, COLLECTION, NUMBER, LITERAL, SYMBOL];
const DEFAULT_CONTAINS = [
LIST,
STRING,
HINT,
HINT_COL,
COMMENT,
KEY,
COLLECTION,
NUMBER,
LITERAL,
SYMBOL
];

var GLOBAL = {
const GLOBAL = {
beginKeywords: globals,
lexemes: SYMBOL_RE,
end: '(\\[|#|\\d|"|:|\\{|\\)|\\(|$)',
Expand All @@ -107,19 +123,34 @@ export default function(hljs) {
excludeEnd: true,
// we can only have a single title
endsParent: true
},
}
].concat(DEFAULT_CONTAINS)
};

LIST.contains = [hljs.COMMENT('comment', ''), GLOBAL, NAME, BODY];
LIST.contains = [
hljs.COMMENT('comment', ''),
GLOBAL,
NAME,
BODY
];
BODY.contains = DEFAULT_CONTAINS;
COLLECTION.contains = DEFAULT_CONTAINS;
HINT_COL.contains = [COLLECTION];
HINT_COL.contains = [ COLLECTION ];

return {
name: 'Clojure',
aliases: ['clj'],
aliases: [ 'clj' ],
illegal: /\S/,
contains: [LIST, STRING, HINT, HINT_COL, COMMENT, KEY, COLLECTION, NUMBER, LITERAL]
contains: [
LIST,
STRING,
HINT,
HINT_COL,
COMMENT,
KEY,
COLLECTION,
NUMBER,
LITERAL
]
};
}
28 changes: 25 additions & 3 deletions src/languages/erlang.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ export default function(hljs) {
TUPLE.contains = BASIC_MODES;
RECORD_ACCESS.contains[1].contains = BASIC_MODES;

const DIRECTIVES = [
"-module",
"-record",
"-undef",
"-export",
"-ifdef",
"-ifndef",
"-author",
"-copyright",
"-doc",
"-vsn",
"-import",
"-include",
"-include_lib",
"-compile",
"-define",
"-else",
"-endif",
"-file",
"-behaviour",
"-behavior",
"-spec"
];

const PARAMS = {
className: 'params',
begin: '\\(',
Expand Down Expand Up @@ -155,9 +179,7 @@ export default function(hljs) {
returnBegin: true,
keywords: {
$pattern: '-' + hljs.IDENT_RE,
keyword: '-module -record -undef -export -ifdef -ifndef -author -copyright -doc -vsn ' +
'-import -include -include_lib -compile -define -else -endif -file -behaviour ' +
'-behavior -spec'
keyword: DIRECTIVES.map(x => `${x}|1.5`).join(" ")
},
contains: [PARAMS]
},
Expand Down
1 change: 1 addition & 0 deletions src/languages/flix.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function(hljs) {

const NAME = {
className: 'title',
relevance: 0,
begin: /[^0-9\n\t "'(),.`{}\[\]:;][^\n\t "'(),.`{}\[\]:;]+|[^0-9\n\t "'(),.`{}\[\]:;=]/
};

Expand Down
2 changes: 1 addition & 1 deletion src/languages/gml.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ export default function(hljs) {
symbol: 'argument_relative argument argument0 argument1 argument2 ' +
'argument3 argument4 argument5 argument6 argument7 argument8 ' +
'argument9 argument10 argument11 argument12 argument13 argument14 ' +
'argument15 argument_count x y xprevious yprevious xstart ystart ' +
'argument15 argument_count x|0 y|0 xprevious yprevious xstart ystart ' +
'hspeed vspeed direction speed friction gravity gravity_direction ' +
'path_index path_position path_positionprevious path_speed ' +
'path_scale path_orientation path_endaction object_index id solid ' +
Expand Down
Loading

0 comments on commit 89f4e84

Please sign in to comment.