Skip to content

Commit a74eb87

Browse files
committed
fix: match api signatures to keep it consistent and removed unused code
1 parent 7d3cb09 commit a74eb87

File tree

4 files changed

+135
-240
lines changed

4 files changed

+135
-240
lines changed

src/editor/CodeHintManager.js

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,27 @@ define(function (require, exports, module) {
450450
return hintList.callMoveUp(callMoveUpEvent);
451451
}
452452

453-
var response = sessionProvider.getHints(lastChar);
453+
var response = null;
454+
455+
// Get hints from regular provider if available
456+
if (sessionProvider) {
457+
response = sessionProvider.getHints(lastChar);
458+
}
459+
454460
lastChar = null;
455461

462+
// we need this to track if we used hintsAtTopHandler as fallback
463+
// because otherwise we will end up calling it twice
464+
var usedTopHintsAsFallback = false;
465+
466+
// If regular provider doesn't have hints, try hints-at-top handler
467+
if (!response && hintsAtTopHandler && hintsAtTopHandler.getHints) {
468+
response = hintsAtTopHandler.getHints(sessionEditor, lastChar);
469+
usedTopHintsAsFallback = true;
470+
}
471+
456472
if (!response) {
457-
// the provider wishes to close the session
473+
// No provider wishes to show hints, close the session
458474
_endSession();
459475
} else {
460476
// if the response is true, end the session and begin another
@@ -465,11 +481,14 @@ define(function (require, exports, module) {
465481
_beginSession(previousEditor);
466482
} else if (response.hasOwnProperty("hints")) { // a synchronous response
467483
// allow extensions to modify the response by adding hints at the top
468-
if (hintsAtTopHandler) {
469-
if (typeof hintsAtTopHandler === 'function') {
470-
response = hintsAtTopHandler(response, sessionEditor);
471-
} else if (hintsAtTopHandler.prepend) {
472-
response = hintsAtTopHandler.prepend(response, sessionEditor);
484+
// BUT only if we didn't already use the top hints as fallback
485+
if (!usedTopHintsAsFallback && sessionProvider && hintsAtTopHandler && hintsAtTopHandler.getHints) {
486+
var topHints = hintsAtTopHandler.getHints(sessionEditor, lastChar);
487+
488+
if (topHints && topHints.hints && topHints.hints.length > 0) {
489+
// Prepend the top hints to the existing response
490+
var combinedHints = topHints.hints.concat(response.hints);
491+
response = $.extend({}, response, { hints: combinedHints });
473492
}
474493
}
475494

@@ -490,11 +509,12 @@ define(function (require, exports, module) {
490509
return;
491510
}
492511
// allow extensions to modify the response by adding hints at the top
493-
if (hintsAtTopHandler) {
494-
if (typeof hintsAtTopHandler === 'function') {
495-
hints = hintsAtTopHandler(hints, sessionEditor);
496-
} else if (hintsAtTopHandler.prepend) {
497-
hints = hintsAtTopHandler.prepend(hints, sessionEditor);
512+
if (sessionProvider && hintsAtTopHandler && hintsAtTopHandler.getHints) {
513+
var topHints = hintsAtTopHandler.getHints(sessionEditor, lastChar);
514+
if (topHints && topHints.hints && topHints.hints.length > 0) {
515+
// Prepend the top hints to the existing response
516+
var combinedHints = topHints.hints.concat(hints.hints);
517+
hints = $.extend({}, hints, { hints: combinedHints });
498518
}
499519
}
500520

@@ -507,7 +527,7 @@ define(function (require, exports, module) {
507527
});
508528
}
509529
}
510-
}
530+
};
511531

512532
/**
513533
* Try to begin a new hinting session.
@@ -529,57 +549,71 @@ define(function (require, exports, module) {
529549
var language = editor.getLanguageForSelection(),
530550
enabledProviders = _getProvidersForLanguageId(language.getId());
531551

532-
enabledProviders.some(function (item, index) {
533-
if (item.provider.hasHints(editor, lastChar)) {
534-
sessionProvider = item.provider;
535-
return true;
536-
}
537-
});
552+
// Check if hints-at-top handler has hints first to avoid duplication
553+
var hasTopHints = false;
554+
if (hintsAtTopHandler && hintsAtTopHandler.hasHints) {
555+
hasTopHints = hintsAtTopHandler.hasHints(editor, lastChar);
556+
}
538557

539-
// If a provider is found, initialize the hint list and update it
540-
if (sessionProvider) {
541-
var insertHintOnTab,
558+
// Find a suitable provider only if hints-at-top handler doesn't have hints
559+
if (!hasTopHints) {
560+
enabledProviders.some(function (item, index) {
561+
if (item.provider.hasHints(editor, lastChar)) {
562+
sessionProvider = item.provider;
563+
return true;
564+
}
565+
});
566+
}
567+
568+
// If a provider is found or top hints are available, initialize the hint list and update it
569+
if (sessionProvider || hasTopHints) {
570+
var insertHintOnTab = PreferencesManager.get("insertHintOnTab"),
542571
maxCodeHints = PreferencesManager.get("maxCodeHints");
543-
if (sessionProvider.insertHintOnTab !== undefined) {
572+
573+
if (sessionProvider && sessionProvider.insertHintOnTab !== undefined) {
544574
insertHintOnTab = sessionProvider.insertHintOnTab;
545-
} else {
546-
insertHintOnTab = PreferencesManager.get("insertHintOnTab");
547575
}
548576

549577
sessionEditor = editor;
550578
hintList = new CodeHintList(sessionEditor, insertHintOnTab, maxCodeHints);
551579
hintList.onHighlight(function ($hint, $hintDescContainer, reason) {
552580
if (hintList.enableDescription && $hintDescContainer && $hintDescContainer.length) {
553581
// If the current hint provider listening for hint item highlight change
554-
if (sessionProvider.onHighlight) {
582+
if (sessionProvider && sessionProvider.onHighlight) {
555583
sessionProvider.onHighlight($hint, $hintDescContainer, reason);
556584
}
557585

558586
// Update the hint description
559-
if (sessionProvider.updateHintDescription) {
587+
if (sessionProvider && sessionProvider.updateHintDescription) {
560588
sessionProvider.updateHintDescription($hint, $hintDescContainer);
561589
}
562590
} else {
563-
if (sessionProvider.onHighlight) {
591+
if (sessionProvider && sessionProvider.onHighlight) {
564592
sessionProvider.onHighlight($hint, undefined, reason);
565593
}
566594
}
567595
});
568596
hintList.onSelect(function (hint) {
569597
// allow extensions to handle special hint selections
570598
var handled = false;
571-
if (hintsAtTopHandler && hintsAtTopHandler.handleHintSelection) {
572-
handled = hintsAtTopHandler.handleHintSelection(hint, sessionEditor, _endSession);
599+
if (hintsAtTopHandler && hintsAtTopHandler.insertHint) {
600+
handled = hintsAtTopHandler.insertHint(hint);
573601
}
574602

575-
if (!handled) {
603+
if (handled) {
604+
// If hints-at-top handler handled it, end the session
605+
_endSession();
606+
} else if (sessionProvider) {
576607
// Regular hint provider handling
577608
var restart = sessionProvider.insertHint(hint),
578609
previousEditor = sessionEditor;
579610
_endSession();
580611
if (restart) {
581612
_beginSession(previousEditor);
582613
}
614+
} else {
615+
// if none of the provider handled it, we just end the session
616+
_endSession();
583617
}
584618
});
585619
hintList.onClose(()=>{
@@ -727,14 +761,13 @@ define(function (require, exports, module) {
727761
}
728762

729763
/**
730-
* Register a handler to modify hints at the top of the hint list.
731-
* This API allows extensions to prepend their own hints to the standard hint list.
764+
* Register a handler to show hints at the top of the hint list.
765+
* This API allows extensions to add their own hints at the top of the standard hint list.
732766
*
733-
* @param {Function|Object} handler - Either a function that takes (response, editor) and returns
734-
* a modified response, or an object with:
735-
* - prepend: function(response, editor) - modify the hint response
736-
* - handleHintSelection: function(hint, editor, endSession) - handle hint selection
737-
* returns true if handled, false otherwise
767+
* @param {Object} handler - A hint provider object with standard methods:
768+
* - hasHints: function(editor, implicitChar) - returns true if hints are available
769+
* - getHints: function(editor, implicitChar) - returns hint response object with hints array
770+
* - insertHint: function(hint) - handles hint insertion, returns true if handled
738771
*/
739772
function showHintsAtTop(handler) {
740773
hintsAtTopHandler = handler;

src/extensionsIntegrated/CustomSnippets/codeHintIntegration.js

Lines changed: 64 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020

2121
define(function (require, exports, module) {
2222
const CodeHintManager = require("editor/CodeHintManager");
23+
const EditorManager = require("editor/EditorManager");
2324

2425
const Global = require("./global");
2526
const Driver = require("./driver");
27+
const Helper = require("./helper");
2628
const SnippetCursorManager = require("./snippetCursorManager");
2729

2830
/**
@@ -31,80 +33,78 @@ define(function (require, exports, module) {
3133
*/
3234
const CustomSnippetsHandler = {
3335
/**
34-
* Prepend custom snippets to the hint response
35-
* @param {Object} response - The original hint response from the current provider
36+
* Determines whether any custom snippets hints are available
3637
* @param {Editor} editor - The current editor instance
37-
* @return {Object} - The modified response with custom snippets added to the front
38+
* @param {string} implicitChar - The last character typed (null if explicit request)
39+
* @return {boolean} - true if hints are available, false otherwise
3840
*/
39-
prepend: function (response, editor) {
40-
if (!response || !response.hints) {
41-
return response;
41+
hasHints: function (editor, implicitChar) {
42+
// We only show hints for implicit requests (when user is typing)
43+
if (implicitChar === null) {
44+
return false;
4245
}
4346

4447
try {
45-
// check if the response already contains custom snippet hints to avoid duplicates
46-
// this is needed because sometimes when there are no default hints present then the
47-
// SnippetCodeHints.js shows some hints, so we don't want to duplicate hints
48-
if (Array.isArray(response.hints) && response.hints.length > 0) {
49-
const hasCustomSnippets = response.hints.some((hint) => {
50-
return (
51-
(hint && hint.hasClass && hint.hasClass("emmet-hint")) ||
52-
(hint && hint.attr && hint.attr("data-isCustomSnippet"))
53-
);
54-
});
55-
56-
if (hasCustomSnippets) {
57-
return response; // already has custom snippets, don't need to add again
58-
}
48+
const needle = Driver.getWordBeforeCursor();
49+
if (!needle || !needle.word) {
50+
return false;
5951
}
6052

61-
const wordInfo = Driver.getWordBeforeCursor();
62-
if (!wordInfo || !wordInfo.word) {
63-
return response;
53+
// Check if there's at least one exact match using language context detection
54+
const hasMatch = Helper.hasExactMatchingSnippet(needle.word.toLowerCase(), editor);
55+
return hasMatch;
56+
} catch (e) {
57+
return false;
58+
}
59+
},
60+
61+
/**
62+
* Get custom snippet hints to show at the top of the hint list
63+
* @param {Editor} editor - The current editor instance
64+
* @param {string} implicitChar - The last character typed (null if explicit request)
65+
* @return {Object} - The hint response object with hints array
66+
*/
67+
getHints: function (editor, implicitChar) {
68+
try {
69+
const needle = Driver.getWordBeforeCursor();
70+
if (!needle || !needle.word) {
71+
return null;
6472
}
6573

66-
const needle = wordInfo.word.toLowerCase();
67-
const Helper = require("./helper");
74+
const word = needle.word.toLowerCase();
6875

69-
// check if there's at least one exact match using language context detection
70-
if (!Helper.hasExactMatchingSnippet(needle, editor)) {
71-
return response;
76+
// Check if there's at least one exact match using language context detection
77+
if (!Helper.hasExactMatchingSnippet(word, editor)) {
78+
return null;
7279
}
7380

74-
// get all matching snippets using language context detection
75-
const matchingSnippets = Helper.getMatchingSnippets(needle, editor);
81+
// Get all matching snippets using language context detection
82+
const matchingSnippets = Helper.getMatchingSnippets(word, editor);
7683

77-
// if we have matching snippets, prepend them to the hints
7884
if (matchingSnippets.length > 0) {
7985
const customSnippetHints = matchingSnippets.map((snippet) => {
80-
return Helper.createHintItem(snippet.abbreviation, needle, snippet.description);
86+
return Helper.createHintItem(snippet.abbreviation, needle.word, snippet.description);
8187
});
8288

83-
// create a new response with custom snippets at the top
84-
const newResponse = $.extend({}, response);
85-
if (Array.isArray(response.hints)) {
86-
newResponse.hints = customSnippetHints.concat(response.hints);
87-
} else {
88-
newResponse.hints = customSnippetHints.concat([response.hints]);
89-
}
90-
91-
return newResponse;
89+
return {
90+
hints: customSnippetHints,
91+
selectInitial: true,
92+
handleWideResults: false
93+
};
9294
}
9395
} catch (e) {
94-
console.log("Error checking custom snippets:", e);
96+
console.log("Error getting custom snippets:", e);
9597
}
9698

97-
return response;
99+
return null;
98100
},
99101

100102
/**
101-
* Handle selection of custom snippet hints
103+
* Handle insertion of custom snippet hints
102104
* @param {jQuery} hint - The selected hint element
103-
* @param {Editor} editor - The current editor instance
104-
* @param {Function} endSession - Function to end the current hint session
105105
* @return {boolean} - true if handled, false otherwise
106106
*/
107-
handleHintSelection: function (hint, editor, endSession) {
107+
insertHint: function (hint) {
108108
// check if the hint is a custom snippet
109109
if (hint && hint.jquery && hint.attr("data-isCustomSnippet")) {
110110
// handle custom snippet insertion
@@ -114,15 +114,23 @@ define(function (require, exports, module) {
114114
(snippet) => snippet.abbreviation === abbreviation
115115
);
116116
if (matchedSnippet) {
117-
// replace the typed abbreviation with the template text using cursor manager
118-
const wordInfo = Driver.getWordBeforeCursor();
119-
const start = { line: wordInfo.line, ch: wordInfo.ch + 1 };
120-
const end = editor.getCursorPos();
121-
122-
SnippetCursorManager.insertSnippetWithTabStops(editor, matchedSnippet.templateText, start, end);
123-
124-
endSession();
125-
return true; // handled
117+
// Get current editor from EditorManager since it's not passed
118+
const editor = EditorManager.getFocusedEditor();
119+
120+
if (editor) {
121+
// replace the typed abbreviation with the template text using cursor manager
122+
const wordInfo = Driver.getWordBeforeCursor();
123+
const start = { line: wordInfo.line, ch: wordInfo.ch + 1 };
124+
const end = editor.getCursorPos();
125+
126+
SnippetCursorManager.insertSnippetWithTabStops(
127+
editor,
128+
matchedSnippet.templateText,
129+
start,
130+
end
131+
);
132+
return true; // handled
133+
}
126134
}
127135
}
128136
}
@@ -140,14 +148,5 @@ define(function (require, exports, module) {
140148
CodeHintManager.showHintsAtTop(CustomSnippetsHandler);
141149
}
142150

143-
/**
144-
* Clean up the integration
145-
* This should be called when the extension is being disabled/unloaded
146-
*/
147-
function cleanup() {
148-
CodeHintManager.clearHintsAtTop();
149-
}
150-
151151
exports.init = init;
152-
exports.cleanup = cleanup;
153152
});

src/extensionsIntegrated/CustomSnippets/main.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ define(function (require, exports, module) {
2828

2929
const Driver = require("./driver");
3030
const SnippetsList = require("./snippetsList");
31-
const SnippetCodeHints = require("./snippetCodeHints");
3231
const CodeHintIntegration = require("./codeHintIntegration");
3332
const Helper = require("./helper");
3433
const UIHelper = require("./UIHelper");
@@ -246,7 +245,6 @@ define(function (require, exports, module) {
246245
CommandManager.register(MENU_ITEM_NAME, MY_COMMAND_ID, showCustomSnippetsPanel);
247246
$snippetsPanel = $(snippetsPanelTpl);
248247
_addToMenu();
249-
SnippetCodeHints.init();
250248
CodeHintIntegration.init();
251249
SnippetsState.loadSnippetsFromState();
252250
SnippetCursorManager.registerHandlers();

0 commit comments

Comments
 (0)