[Console] Dont show autocomplete within comment blocks (#201543)

This commit is contained in:
Ignacio Rivas 2024-11-29 14:39:24 +01:00 committed by GitHub
parent d5cf0a6be4
commit 648c323d1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 67 additions and 0 deletions

View file

@ -411,13 +411,44 @@ export class MonacoEditorActionsProvider {
return getDocumentationLinkFromAutocomplete(request, docLinkVersion);
}
private isInsideMultilineComment(model: monaco.editor.ITextModel, lineNumber: number): boolean {
let insideComment = false;
for (let i = 1; i <= lineNumber; i++) {
const lineContent = model.getLineContent(i).trim();
if (lineContent.startsWith('/*')) {
insideComment = true;
}
if (lineContent.includes('*/')) {
insideComment = false;
}
}
return insideComment;
}
private async getAutocompleteType(
model: monaco.editor.ITextModel,
{ lineNumber, column }: monaco.Position
): Promise<AutocompleteType | null> {
// Get the content of the current line up until the cursor position
const currentLineContent = model.getLineContent(lineNumber);
const trimmedContent = currentLineContent.trim();
// If we are positioned inside a comment block, no autocomplete should be provided
if (
trimmedContent.startsWith('#') ||
trimmedContent.startsWith('//') ||
trimmedContent.startsWith('/*') ||
trimmedContent.startsWith('*') ||
trimmedContent.includes('*/') ||
this.isInsideMultilineComment(model, lineNumber)
) {
return null;
}
// get the current request on this line
const currentRequests = await this.getRequestsBetweenLines(model, lineNumber, lineNumber);
const currentRequest = currentRequests.at(0);
// if there is no request, suggest method
if (!currentRequest) {
return AutocompleteType.METHOD;

View file

@ -399,5 +399,41 @@ GET _search
expect(await PageObjects.console.getAutocompleteSuggestion(1)).to.be.eql(undefined);
});
});
describe('Autocomplete shouldnt trigger within', () => {
beforeEach(async () => {
await PageObjects.console.skipTourIfExists();
await PageObjects.console.clearEditorText();
});
it('a hash comment', async () => {
await PageObjects.console.enterText(`# GET /`);
await PageObjects.console.sleepForDebouncePeriod();
expect(PageObjects.console.isAutocompleteVisible()).to.be.eql(false);
});
it('a simple double slash comment', async () => {
await PageObjects.console.enterText(`// GET /`);
await PageObjects.console.sleepForDebouncePeriod();
expect(PageObjects.console.isAutocompleteVisible()).to.be.eql(false);
});
it('a single line block comment', async () => {
await PageObjects.console.enterText(`/* GET /`);
await PageObjects.console.sleepForDebouncePeriod();
expect(PageObjects.console.isAutocompleteVisible()).to.be.eql(false);
});
it('a multiline block comment', async () => {
await PageObjects.console.enterText(`/*
GET /`);
await PageObjects.console.sleepForDebouncePeriod();
expect(PageObjects.console.isAutocompleteVisible()).to.be.eql(false);
});
});
});
}