Improve debouncing logic to immediately react to language changes

Fixes #148196
This commit is contained in:
Jackson Kearl 2022-04-26 16:00:51 -07:00
parent 06f498c783
commit ac3e081e0e
No known key found for this signature in database
GPG key ID: DA09A59C409FC400

View file

@ -3,9 +3,9 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Delayer } from 'vs/base/common/async';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Disposable } from 'vs/base/common/lifecycle';
import { ResourceMap } from 'vs/base/common/map';
import { URI } from 'vs/base/common/uri';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { localize } from 'vs/nls';
@ -59,7 +59,11 @@ class CellStatusBarLanguageDetectionProvider implements INotebookCellStatusBarIt
readonly viewType = '*';
private delayer = new Delayer<INotebookCellStatusBarItemList | undefined>(500);
private cache = new ResourceMap<{
lastUpdate: number;
lastCellLang: string;
lastGuess?: string;
}>();
constructor(
@INotebookService private readonly _notebookService: INotebookService,
@ -71,53 +75,57 @@ class CellStatusBarLanguageDetectionProvider implements INotebookCellStatusBarIt
) { }
async provideCellStatusBarItems(uri: URI, index: number, token: CancellationToken): Promise<INotebookCellStatusBarItemList | undefined> {
return await this.delayer.trigger(async () => {
const doc = this._notebookService.getNotebookTextModel(uri);
const cell = doc?.cells[index];
if (!cell || token.isCancellationRequested) {
return;
}
const doc = this._notebookService.getNotebookTextModel(uri);
const cell = doc?.cells[index];
if (!cell) { return; }
const enablementConfig = this._configurationService.getValue('workbench.editor.languageDetectionHints');
const enabled = enablementConfig === 'always' || enablementConfig === 'notebookEditors';
if (!enabled) {
return;
}
const enablementConfig = this._configurationService.getValue('workbench.editor.languageDetectionHints');
const enabled = enablementConfig === 'always' || enablementConfig === 'notebookEditors';
if (!enabled) {
return;
}
const currentLanguageId = cell.cellKind === CellKind.Markup ?
'markdown' :
(this._languageService.getLanguageIdByLanguageName(cell.language) || cell.language);
const currentLanguageId = cell.cellKind === CellKind.Markup ?
'markdown' :
(this._languageService.getLanguageIdByLanguageName(cell.language) || cell.language);
if (!this.cache.has(uri)) {
this.cache.set(uri, { lastCellLang: currentLanguageId, lastUpdate: 0 });
}
const cached = this.cache.get(uri)!;
if (cached.lastUpdate < Date.now() - 1000 || cached.lastCellLang !== currentLanguageId) {
cached.lastUpdate = Date.now();
cached.lastCellLang = currentLanguageId;
const kernel = this._notebookKernelService.getSelectedOrSuggestedKernel(doc);
const items: INotebookCellStatusBarItem[] = [];
if (kernel) {
const availableLangs = [];
availableLangs.push(...kernel.supportedLanguages, 'markdown');
const detectedLanguageId = await this._languageDetectionService.detectLanguage(cell.uri, availableLangs);
if (detectedLanguageId && currentLanguageId !== detectedLanguageId) {
const detectedName = this._languageService.getLanguageName(detectedLanguageId) || detectedLanguageId;
let tooltip = localize('notebook.cell.status.autoDetectLanguage', "Accept Detected Language: {0}", detectedName);
const keybinding = this._keybindingService.lookupKeybinding(DETECT_CELL_LANGUAGE);
const label = keybinding?.getLabel();
if (label) {
tooltip += ` (${label})`;
}
items.push({
text: '$(lightbulb-autofix)',
command: DETECT_CELL_LANGUAGE,
tooltip,
alignment: CellStatusbarAlignment.Right,
priority: -Number.MAX_SAFE_INTEGER + 1
});
}
cached.lastGuess = await this._languageDetectionService.detectLanguage(cell.uri, availableLangs);
}
}
return { items };
});
const items: INotebookCellStatusBarItem[] = [];
if (cached.lastGuess && currentLanguageId !== cached.lastGuess) {
const detectedName = this._languageService.getLanguageName(cached.lastGuess) || cached.lastGuess;
let tooltip = localize('notebook.cell.status.autoDetectLanguage', "Accept Detected Language: {0}", detectedName);
const keybinding = this._keybindingService.lookupKeybinding(DETECT_CELL_LANGUAGE);
const label = keybinding?.getLabel();
if (label) {
tooltip += ` (${label})`;
}
items.push({
text: '$(lightbulb-autofix)',
command: DETECT_CELL_LANGUAGE,
tooltip,
alignment: CellStatusbarAlignment.Right,
priority: -Number.MAX_SAFE_INTEGER + 1
});
}
return { items };
}
}