Skip to content

Commit d4d7bda

Browse files
committed
feat: display hints for emmet abbr that can be expanded
1 parent 9253416 commit d4d7bda

File tree

1 file changed

+62
-79
lines changed
  • src/extensionsIntegrated/Emmet

1 file changed

+62
-79
lines changed

src/extensionsIntegrated/Emmet/main.js

Lines changed: 62 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ define(function (require, exports, module) {
33
const EditorManager = require("editor/EditorManager");
44
const PreferencesManager = require("preferences/PreferencesManager");
55
const Strings = require("strings");
6+
const CodeHintManager = require("editor/CodeHintManager");
67

78
const EXPAND_ABBR = Phoenix.libs.Emmet.expand;
89
// const EMMET = Phoenix.libs.Emmet.module;
@@ -234,6 +235,64 @@ define(function (require, exports, module) {
234235
description: Strings.DESCRIPTION_EMMET
235236
});
236237

238+
239+
/**
240+
* @constructor
241+
*/
242+
function EmmetMarkupHints() {
243+
}
244+
245+
EmmetMarkupHints.prototype.hasHints = function (editor, implicitChar) {
246+
247+
this.editor = editor;
248+
249+
const wordObj = getWordBeforeCursor(editor);
250+
const config = createConfig(editor);
251+
if (config && config.syntax === "html") {
252+
253+
// make sure we donot have empty spaces
254+
if (wordObj.word.trim()) {
255+
256+
const expandedAbbr = isExpandable(editor, wordObj.word, config);
257+
if (expandedAbbr) {
258+
return true;
259+
}
260+
}
261+
}
262+
263+
return false;
264+
};
265+
266+
267+
EmmetMarkupHints.prototype.getHints = function (implicitChar) {
268+
const wordObj = getWordBeforeCursor(this.editor);
269+
const config = createConfig(this.editor);
270+
271+
const expandedAbbr = isExpandable(this.editor, wordObj.word, config);
272+
if (!expandedAbbr) {
273+
return null;
274+
}
275+
276+
const result = [wordObj.word];
277+
278+
return {
279+
hints: result,
280+
match: null,
281+
selectInitial: true,
282+
defaultDescriptionWidth: true,
283+
handleWideResults: false
284+
};
285+
};
286+
287+
EmmetMarkupHints.prototype.insertHint = function (completion) {
288+
const wordObj = getWordBeforeCursor(this.editor);
289+
const config = createConfig(this.editor);
290+
const expandedAbbr = isExpandable(this.editor, wordObj.word, config);
291+
updateAbbrInEditor(this.editor, wordObj, expandedAbbr);
292+
return false;
293+
};
294+
295+
237296
/**
238297
* Responsible to create the configuration based on the file type.
239298
* Config is an object with two properties, type & snytax.
@@ -551,84 +610,6 @@ define(function (require, exports, module) {
551610
}
552611

553612

554-
/**
555-
* Responsible to handle the flow of the program
556-
*
557-
* @param {Editor} editor - the editor instance
558-
* @param {Object} keyboardEvent - the keyboard event object
559-
*/
560-
function driver(editor, keyboardEvent) {
561-
const config = createConfig(editor);
562-
563-
if (config) {
564-
565-
// to make sure it is an html file
566-
if (config.syntax === "html") {
567-
const wordObj = getWordBeforeCursor(editor);
568-
569-
// make sure we donot have empty spaces
570-
if (wordObj.word.trim()) {
571-
572-
const expandedAbbr = isExpandable(editor, wordObj.word, config);
573-
if (expandedAbbr) {
574-
updateAbbrInEditor(editor, wordObj, expandedAbbr);
575-
576-
// prevent the default working of the 'tab' key
577-
keyboardEvent.preventDefault();
578-
}
579-
}
580-
}
581-
}
582-
}
583-
584-
/**
585-
* Function that gets triggered when any key is pressed.
586-
* We only want to look for 'tab' key events
587-
*
588-
* @param {Event} event - unused event detail
589-
* @param {Editor} editor - the editor instance
590-
* @param {Object} keyboardEvent - an object that has properties related to the keyboard,
591-
* mainly the key that is pressed (keyboardEvent.key)
592-
* @returns {Boolean} True if abbreviation is expanded else false
593-
*/
594-
function handleKeyEvent(event, editor, keyboardEvent) {
595-
if (!enabled) {
596-
return false;
597-
}
598-
599-
// if not a 'tab' key press, ignore
600-
if (keyboardEvent.key !== "Tab") {
601-
return false;
602-
}
603-
604-
// the function that drives the flow of the program
605-
driver(editor, keyboardEvent);
606-
}
607-
608-
/**
609-
* Register all the required handlers
610-
*/
611-
function registerHandlers() {
612-
// Get the current active editor and attach the change listener
613-
const activeEditor = EditorManager.getActiveEditor();
614-
if (activeEditor) {
615-
activeEditor.on("keydown", handleKeyEvent);
616-
}
617-
618-
// Listen for active editor changes, to attach the handler to new editor
619-
EditorManager.on("activeEditorChange", function (event, newEditor, oldEditor) {
620-
if (oldEditor) {
621-
// Remove listener from old editor
622-
oldEditor.off("keydown", handleKeyEvent);
623-
}
624-
if (newEditor) {
625-
// Add listener to new editor
626-
newEditor.off("change", handleKeyEvent);
627-
newEditor.on("keydown", handleKeyEvent);
628-
}
629-
});
630-
}
631-
632613
/**
633614
* Checks for preference changes, to enable/disable the feature
634615
*/
@@ -642,7 +623,9 @@ define(function (require, exports, module) {
642623
PreferencesManager.on("change", PREFERENCES_EMMET, preferenceChanged);
643624
preferenceChanged();
644625

645-
registerHandlers();
626+
var emmetMarkupHints = new EmmetMarkupHints();
627+
CodeHintManager.registerHintProvider(emmetMarkupHints, ["html"], 2);
628+
646629
});
647630
});
648631

0 commit comments

Comments
 (0)