editor: autocomplete on typing

This commit is contained in:
tophf 2017-06-18 15:20:48 +03:00
parent 7443a4cb89
commit 2240a0895e
4 changed files with 49 additions and 0 deletions

View File

@ -91,6 +91,10 @@
"message": "Checking...", "message": "Checking...",
"description": "Text to display when checking a style for an update" "description": "Text to display when checking a style for an update"
}, },
"cm_autocompleteOnTyping": {
"message": "Autocomplete on typing",
"description": "Label for the checkbox in the style editor."
},
"cm_indentWithTabs": { "cm_indentWithTabs": {
"message": "Use tabs with smart indentation", "message": "Use tabs with smart indentation",
"description": "Label for the checkbox controlling tabs with smart indentation option for the style editor." "description": "Label for the checkbox controlling tabs with smart indentation option for the style editor."

View File

@ -704,6 +704,10 @@
<input id="editor.indentWithTabs" type="checkbox"> <input id="editor.indentWithTabs" type="checkbox">
<label id="indentWithTabs-label" for="editor.indentWithTabs" i18n-text="cm_indentWithTabs"></label> <label id="indentWithTabs-label" for="editor.indentWithTabs" i18n-text="cm_indentWithTabs"></label>
</div> </div>
<div class="option">
<input id="editor.autocompleteOnTyping" type="checkbox">
<label for="editor.autocompleteOnTyping" i18n-text="cm_autocompleteOnTyping"></label>
</div>
<div class="option aligned"> <div class="option aligned">
<label id="tabSize-label" for="editor.tabSize" i18n-text="cm_tabSize"></label> <label id="tabSize-label" for="editor.tabSize" i18n-text="cm_tabSize"></label>
<input id="editor.tabSize" type="number" min="0"> <input id="editor.tabSize" type="number" min="0">

40
edit.js
View File

@ -161,6 +161,7 @@ function initCodeMirror() {
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"], gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"],
matchBrackets: true, matchBrackets: true,
highlightSelectionMatches: {showToken: /[#.\-\w]/, annotateScrollbar: true}, highlightSelectionMatches: {showToken: /[#.\-\w]/, annotateScrollbar: true},
hintOptions: {},
lint: {getAnnotations: CodeMirror.lint.css, delay: prefs.get("editor.lintDelay")}, lint: {getAnnotations: CodeMirror.lint.css, delay: prefs.get("editor.lintDelay")},
lintReportDelay: prefs.get("editor.lintReportDelay"), lintReportDelay: prefs.get("editor.lintReportDelay"),
styleActiveLine: true, styleActiveLine: true,
@ -319,6 +320,13 @@ function acmeEventListener(event) {
}, 100); }, 100);
})(); })();
return; return;
case 'autocompleteOnTyping':
editors.forEach(cm => {
const onOff = el.checked ? 'on' : 'off';
cm[onOff]('change', autocompleteOnTyping);
cm[onOff]('pick', autocompletePicked);
});
return;
case "matchHighlight": case "matchHighlight":
switch (value) { switch (value) {
case 'token': case 'token':
@ -338,6 +346,10 @@ function setupCodeMirror(textarea, index) {
var cm = CodeMirror.fromTextArea(textarea, {lint: null}); var cm = CodeMirror.fromTextArea(textarea, {lint: null});
cm.on("change", indicateCodeChange); cm.on("change", indicateCodeChange);
if (prefs.get('editor.autocompleteOnTyping')) {
cm.on('change', autocompleteOnTyping);
cm.on('pick', autocompletePicked);
}
cm.on("blur", function(cm) { cm.on("blur", function(cm) {
editors.lastActive = cm; editors.lastActive = cm;
hotkeyRerouter.setState(true); hotkeyRerouter.setState(true);
@ -874,6 +886,34 @@ function toggleStyle() {
save(); save();
} }
function autocompleteOnTyping(cm, info, debounced) {
if (cm.state.completionActive
|| info.origin && !info.origin.includes('input')
|| !info.text.last) {
return;
}
if (cm.state.autocompletePicked) {
cm.state.autocompletePicked = false;
return;
}
if (!debounced) {
debounce(autocompleteOnTyping, 100, cm, info, true);
return;
}
if (info.text.last.match(/[-\w!]+$/)) {
cm.state.autocompletePicked = false;
cm.options.hintOptions.completeSingle = false;
cm.execCommand('autocomplete');
setTimeout(() => {
cm.options.hintOptions.completeSingle = true;
});
}
}
function autocompletePicked(cm) {
cm.state.autocompletePicked = true;
}
function refocusMinidialog(cm) { function refocusMinidialog(cm) {
var section = cm.getSection(); var section = cm.getSection();
if (!section.querySelector(".CodeMirror-dialog")) { if (!section.querySelector(".CodeMirror-dialog")) {

View File

@ -44,6 +44,7 @@ var prefs = new function Prefs() {
'editor.matchHighlight': 'token', // token = token/word under cursor even if nothing is selected 'editor.matchHighlight': 'token', // token = token/word under cursor even if nothing is selected
// selection = only when something is selected // selection = only when something is selected
// '' (empty string) = disabled // '' (empty string) = disabled
'editor.autocompleteOnTyping': false, // show autocomplete dropdown on typing a word token
'editor.contextDelete': contextDeleteMissing(), // "Delete" item in context menu 'editor.contextDelete': contextDeleteMissing(), // "Delete" item in context menu
'badgeDisabled': '#8B0000', // badge background color when disabled 'badgeDisabled': '#8B0000', // badge background color when disabled