Skip to content

Commit 28de164

Browse files
authored
Merge branch 'main' into pluto/wk-fix
2 parents 1426375 + 1b63546 commit 28de164

File tree

16 files changed

+3685
-25
lines changed

16 files changed

+3685
-25
lines changed

src/editor/CodeHintManager.js

Lines changed: 108 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ define(function (require, exports, module) {
266266
codeHintsEnabled = true,
267267
codeHintOpened = false;
268268

269+
// API for extensions to show hints at the top
270+
let hintsAtTopHandler = null;
271+
269272
PreferencesManager.definePreference("showCodeHints", "boolean", true, {
270273
description: Strings.DESCRIPTION_SHOW_CODE_HINTS
271274
});
@@ -447,11 +450,27 @@ define(function (require, exports, module) {
447450
return hintList.callMoveUp(callMoveUpEvent);
448451
}
449452

450-
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+
451460
lastChar = null;
452461

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+
453472
if (!response) {
454-
// the provider wishes to close the session
473+
// No provider wishes to show hints, close the session
455474
_endSession();
456475
} else {
457476
// if the response is true, end the session and begin another
@@ -461,6 +480,18 @@ define(function (require, exports, module) {
461480
_endSession();
462481
_beginSession(previousEditor);
463482
} else if (response.hasOwnProperty("hints")) { // a synchronous response
483+
// allow extensions to modify the response by adding hints at the top
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 });
492+
}
493+
}
494+
464495
if (hintList.isOpen()) {
465496
// the session is open
466497
hintList.update(response);
@@ -477,6 +508,15 @@ define(function (require, exports, module) {
477508
if (!hintList) {
478509
return;
479510
}
511+
// allow extensions to modify the response by adding hints at the top
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 });
518+
}
519+
}
480520

481521
if (hintList.isOpen()) {
482522
// the session is open
@@ -487,7 +527,7 @@ define(function (require, exports, module) {
487527
});
488528
}
489529
}
490-
}
530+
};
491531

492532
/**
493533
* Try to begin a new hinting session.
@@ -509,48 +549,71 @@ define(function (require, exports, module) {
509549
var language = editor.getLanguageForSelection(),
510550
enabledProviders = _getProvidersForLanguageId(language.getId());
511551

512-
enabledProviders.some(function (item, index) {
513-
if (item.provider.hasHints(editor, lastChar)) {
514-
sessionProvider = item.provider;
515-
return true;
516-
}
517-
});
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+
}
518557

519-
// If a provider is found, initialize the hint list and update it
520-
if (sessionProvider) {
521-
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"),
522571
maxCodeHints = PreferencesManager.get("maxCodeHints");
523-
if (sessionProvider.insertHintOnTab !== undefined) {
572+
573+
if (sessionProvider && sessionProvider.insertHintOnTab !== undefined) {
524574
insertHintOnTab = sessionProvider.insertHintOnTab;
525-
} else {
526-
insertHintOnTab = PreferencesManager.get("insertHintOnTab");
527575
}
528576

529577
sessionEditor = editor;
530578
hintList = new CodeHintList(sessionEditor, insertHintOnTab, maxCodeHints);
531579
hintList.onHighlight(function ($hint, $hintDescContainer, reason) {
532580
if (hintList.enableDescription && $hintDescContainer && $hintDescContainer.length) {
533581
// If the current hint provider listening for hint item highlight change
534-
if (sessionProvider.onHighlight) {
582+
if (sessionProvider && sessionProvider.onHighlight) {
535583
sessionProvider.onHighlight($hint, $hintDescContainer, reason);
536584
}
537585

538586
// Update the hint description
539-
if (sessionProvider.updateHintDescription) {
587+
if (sessionProvider && sessionProvider.updateHintDescription) {
540588
sessionProvider.updateHintDescription($hint, $hintDescContainer);
541589
}
542590
} else {
543-
if (sessionProvider.onHighlight) {
591+
if (sessionProvider && sessionProvider.onHighlight) {
544592
sessionProvider.onHighlight($hint, undefined, reason);
545593
}
546594
}
547595
});
548596
hintList.onSelect(function (hint) {
549-
var restart = sessionProvider.insertHint(hint),
550-
previousEditor = sessionEditor;
551-
_endSession();
552-
if (restart) {
553-
_beginSession(previousEditor);
597+
// allow extensions to handle special hint selections
598+
var handled = false;
599+
if (hintsAtTopHandler && hintsAtTopHandler.insertHint) {
600+
handled = hintsAtTopHandler.insertHint(hint);
601+
}
602+
603+
if (handled) {
604+
// If hints-at-top handler handled it, end the session
605+
_endSession();
606+
} else if (sessionProvider) {
607+
// Regular hint provider handling
608+
var restart = sessionProvider.insertHint(hint),
609+
previousEditor = sessionEditor;
610+
_endSession();
611+
if (restart) {
612+
_beginSession(previousEditor);
613+
}
614+
} else {
615+
// if none of the provider handled it, we just end the session
616+
_endSession();
554617
}
555618
});
556619
hintList.onClose(()=>{
@@ -697,6 +760,26 @@ define(function (require, exports, module) {
697760
return (hintList && hintList.isOpen());
698761
}
699762

763+
/**
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.
766+
*
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
771+
*/
772+
function showHintsAtTop(handler) {
773+
hintsAtTopHandler = handler;
774+
}
775+
776+
/**
777+
* Unregister the hints at top handler.
778+
*/
779+
function clearHintsAtTop() {
780+
hintsAtTopHandler = null;
781+
}
782+
700783
/**
701784
* Explicitly start a new session. If we have an existing session,
702785
* then close the current one and restart a new one.
@@ -780,6 +863,8 @@ define(function (require, exports, module) {
780863
exports.isOpen = isOpen;
781864
exports.registerHintProvider = registerHintProvider;
782865
exports.hasValidExclusion = hasValidExclusion;
866+
exports.showHintsAtTop = showHintsAtTop;
867+
exports.clearHintsAtTop = clearHintsAtTop;
783868

784869
exports.SELECTION_REASON = CodeHintListModule._SELECTION_REASON;
785870
});

0 commit comments

Comments
 (0)