Skip to content

Commit

Permalink
fix!: autocompletion could not be called consecutively
Browse files Browse the repository at this point in the history
  • Loading branch information
F0rce committed Aug 26, 2022
1 parent f1853af commit f0ca944
Showing 1 changed file with 113 additions and 93 deletions.
206 changes: 113 additions & 93 deletions lit-ace.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ class LitAce extends LitElement {
displayIndentGuides: { type: Boolean },
highlightSelectedWord: { type: Boolean },
useWorker: { type: Boolean },
customAutocompletion: { type: String },
marker: { type: String }, // TODO: remove this, use markerList instead
markerList: { type: Array }, // TODO: kepp this, backend should create a json with all markers
rmMarker: { type: String }, // TODO: use method
statusbarEnabled: { type: Boolean },
enableSnippets: { type: Boolean },
dynamicAutocompletion: { type: String },
};
}

Expand Down Expand Up @@ -470,33 +468,6 @@ class LitAce extends LitElement {
this.editor.renderer.setShowGutter(this.showGutter);
}

customAutocompletionChanged() {
if (this.editor == undefined) {
return;
}

const parsed = JSON.parse(this.customAutocompletion);
const category = parsed.category;
const wordlist = parsed.wordlist;

var staticWordCompleter = {
getCompletions: function (editor, session, pos, prefix, callback) {
callback(
null,
wordlist.map(function (word) {
return { name: word, value: word, score: 10, meta: category };
})
);
},
};

if (parsed.keepcompleters) {
this.editor.completers.push(staticWordCompleter);
} else {
this.editor.completers = [staticWordCompleter];
}
}

markerChanged() {
if (this.editor == undefined) {
return;
Expand Down Expand Up @@ -611,70 +582,6 @@ class LitAce extends LitElement {
}
}

dynamicAutocompletionChanged() {
if (this.editor == undefined) {
return;
}

const parsed = JSON.parse(this.dynamicAutocompletion);
const seperator = parsed.seperator;
const list = parsed.list;
const keys = Object.keys(list);
const defaultCallback = keys.map(function (word) {
return {
name: word,
value: word,
score: 50,
meta: parsed.category,
};
});

var dynamicCompletion = {
getCompletions: function (editor, session, pos, prefix, callback) {
var curLine = session.getDocument().getLine(pos.row);
var curTokens = curLine.slice(0, pos.column).split(/\s+/);
var curCmd = curTokens[curTokens.length - 1];
if (!curCmd) {
callback(null, defaultCallback);
return;
}
const seperatorReplaced = seperator.replace(
/[-\/\\^$*+?.()|[\]{}]/g,
"\\$&"
);
var canidates = [];
const match = curCmd.match(
new RegExp(`(${keys.join("|")}){1}(?=${seperatorReplaced})`, "i")
);
if (match) {
const keyword = match[0];
for (var option of list[keyword]) {
canidates.push(keyword + seperator + option);
}
callback(
null,
canidates.map(function (option) {
return {
name: option,
value: option,
score: 100,
meta: keyword,
};
})
);
} else {
callback(null, defaultCallback);
}
},
};

if (parsed.keepcompleters) {
this.editor.completers.push(dynamicCompletion);
} else {
this.editor.completers = [dynamicCompletion];
}
}

editorBlurChangeAction() {
this.dispatchEvent(
new CustomEvent("editor-blur", {
Expand Down Expand Up @@ -1221,6 +1128,119 @@ class LitAce extends LitElement {
this.editor.statusbar.updateStatus(this.editor, this._statusbarIndex);
}

addStaticWordCompleter(json) {
if (this.editor == undefined) {
this.addEventListener(
"editor-ready",
() => this._addStaticWordCompleter(json),
{
once: true,
}
);
} else {
this._addStaticWordCompleter(json);
}
}

/** @private */
_addStaticWordCompleter(json) {
const parsed = JSON.parse(json);
const category = parsed.category;
const words = parsed.words;

var staticWordCompleter = {
getCompletions: function (editor, session, pos, prefix, callback) {
callback(
null,
words.map(function (word) {
return { name: word, value: word, score: 10, meta: category };
})
);
},
};

if (parsed.keepCompleters) {
this.editor.completers.push(staticWordCompleter);
} else {
this.editor.completers = [staticWordCompleter];
}
}

addDynamicWordCompleter(json) {
if (this.editor == undefined) {
this.addEventListener(
"editor-ready",
() => this._addDynamicWordCompleter(json),
{
once: true,
}
);
} else {
this._addDynamicWordCompleter(json);
}
}

/** @private */
_addDynamicWordCompleter(json) {
const parsed = JSON.parse(json);
const seperator = parsed.seperator;
const list = parsed.dynamicWords;
const keys = Object.keys(list);
const defaultCallback = keys.map(function (word) {
return {
name: word,
value: word,
score: 50,
meta: parsed.category,
};
});

var dynamicCompletion = {
getCompletions: function (editor, session, pos, prefix, callback) {
var curLine = session.getDocument().getLine(pos.row);
var curTokens = curLine.slice(0, pos.column).split(/\s+/);
var curCmd = curTokens[curTokens.length - 1];
if (!curCmd) {
callback(null, defaultCallback);
return;
}
const seperatorReplaced = seperator.replace(
/[-\/\\^$*+?.()|[\]{}]/g,
"\\$&"
);
var canidates = [];
const match = curCmd.match(
new RegExp(`(${keys.join("|")}){1}(?=${seperatorReplaced})`, "i")
);
if (match) {
const keyword = match[0];
for (var option of list[keyword]) {
canidates.push(keyword + seperator + option);
}
callback(
null,
canidates.map(function (option) {
return {
name: option,
value: option,
score: 100,
meta: keyword,
};
})
);
} else {
callback(null, defaultCallback);
}
},
};

if (parsed.keepCompleters) {
this.editor.completers.push(dynamicCompletion);
} else {
this.editor.completers = [dynamicCompletion];
}
}

/** @private */
_createSelectionObject() {
let editor = this.editor;
Expand Down

0 comments on commit f0ca944

Please sign in to comment.