Skip to content

Commit

Permalink
[Console] Dont show autocomplete within comment blocks (#201543)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabarasaba authored Nov 29, 2024
1 parent d5cf0a6 commit 648c323
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions test/functional/apps/console/_autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
}

0 comments on commit 648c323

Please sign in to comment.