Skip to content

Commit

Permalink
[8.17] [Console] Dont show autocomplete within comment blocks (elasti…
Browse files Browse the repository at this point in the history
…c#201543) (elastic#202311)

# Backport

This will backport the following commits from `main` to `8.17`:
- [[Console] Dont show autocomplete within comment blocks
(elastic#201543)](elastic#201543)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Ignacio
Rivas","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-29T13:39:24Z","message":"[Console]
Dont show autocomplete within comment blocks
(elastic#201543)","sha":"648c323d1bbedcabee72a77b39ab3715ad3767ca","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Console","Team:Kibana
Management","release_note:skip","v9.0.0","backport:prev-minor","v8.16.0","v8.17.0"],"title":"[Console]
Dont show autocomplete within comment
blocks","number":201543,"url":"https://github.com/elastic/kibana/pull/201543","mergeCommit":{"message":"[Console]
Dont show autocomplete within comment blocks
(elastic#201543)","sha":"648c323d1bbedcabee72a77b39ab3715ad3767ca"}},"sourceBranch":"main","suggestedTargetBranches":["8.16","8.17"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/201543","number":201543,"mergeCommit":{"message":"[Console]
Dont show autocomplete within comment blocks
(elastic#201543)","sha":"648c323d1bbedcabee72a77b39ab3715ad3767ca"}},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.17","label":"v8.17.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Ignacio Rivas <[email protected]>
  • Loading branch information
kibanamachine and sabarasaba authored Nov 29, 2024
1 parent 9f74c65 commit 5fd3b1e
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 @@ -378,5 +378,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 5fd3b1e

Please sign in to comment.