Skip to content

Commit 24992c1

Browse files
committed
Add snippet support for LaTeX and custom macro completer.
The snippets are copied from the snippets for TeX. The current keywordCompleter for LaTeX is unused because the highlight rules do not give a list of keywords, but color every LaTeX macro, so the completer cannot use the rules to provide completion. This macro completer is inspired from the text completer, but complete only macros (the current text completer do not consider \ to be a part of a word).
1 parent 0207e6d commit 24992c1

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/mode/latex.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22

3+
const { triggerAutocomplete } = require("../autocomplete/util");
34
var oop = require("../lib/oop");
45
var TextMode = require("./text").Mode;
56
var LatexHighlightRules = require("./latex_highlight_rules").LatexHighlightRules;
@@ -19,6 +20,8 @@ oop.inherits(Mode, TextMode);
1920
this.lineCommentStart = "%";
2021

2122
this.$id = "ace/mode/latex";
23+
24+
this.snippetFileId = "ace/snippets/latex";
2225

2326
this.getMatching = function(session, row, column) {
2427
if (row == undefined)
@@ -35,6 +38,49 @@ oop.inherits(Mode, TextMode);
3538
return this.foldingRules.latexBlock(session, row, column, true);
3639
}
3740
};
41+
42+
function wordDistances(doc, pos) {
43+
var macroName = /\\[a-zA-Z0-9]+/g;
44+
45+
var textBefore = doc.getTextRange(Range.fromPoints({
46+
row: 0,
47+
column: 0
48+
}, pos));
49+
var prefixPos = [...textBefore.matchAll(macroName)].length - 1;
50+
51+
var words = [...doc.getValue().matchAll(macroName)];
52+
var wordScores = Object.create(null);
53+
54+
var currentWord = words[prefixPos];
55+
56+
words.forEach(function (word, idx) {
57+
if (!word || word === currentWord) return;
58+
59+
var distance = Math.abs(prefixPos - idx);
60+
var score = words.length - distance;
61+
wordScores[word] = Math.max(score, wordScores[word] ?? 0);
62+
});
63+
return wordScores;
64+
}
65+
66+
this.completer = {
67+
getCompletions: (editor, session, pos, prefix, callback) => {
68+
var wordScores = wordDistances(session, pos);
69+
var wordList = Object.keys(wordScores);
70+
callback(null, wordList.map(function (word) {
71+
return {
72+
caption: word,
73+
value: word,
74+
score: wordScores[word],
75+
meta: "macro",
76+
completer: this,
77+
completerId: this.id,
78+
};
79+
}, this));
80+
},
81+
triggerCharacters: ["\\"],
82+
id: "latexMacroCompleter",
83+
};
3884
}).call(Mode.prototype);
3985

4086
exports.Mode = Mode;

src/snippets/latex.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
3+
exports.snippetText = require("./latex.snippets");
4+
exports.scope = "latex";

src/snippets/latex.snippets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./tex.snippets");

0 commit comments

Comments
 (0)