Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Selection auto complete] Suggest "+" after digits. #28121

Merged
merged 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ incompleteExpr:
| leftParenToken postLogicalOperatorWhitespace expr # UnclosedParenthesizedExpression
| expressionLessParenthesizedExpr # ExpressionlessParenthesizedExpressionWrapper
| leftParenToken postLogicalOperatorWhitespace # UnclosedExpressionlessParenthesizedExpression
| PLUS postNeighborTraversalWhitespace # IncompletePlusTraversalExpression
| DIGITS? PLUS postNeighborTraversalWhitespace # IncompletePlusTraversalExpression
| DIGITS postDigitsWhitespace # IncompleteUpTraversalExpression
| colonToken attributeValue postExpressionWhitespace # IncompleteAttributeExpressionMissingKey;

expressionLessParenthesizedExpr:
Expand Down Expand Up @@ -97,6 +98,8 @@ postUpwardTraversalWhitespace: WS*;

postDownwardTraversalWhitespace: WS*;

postDigitsWhitespace: WS*;

// Value can be a quoted string, unquoted string, or identifier
value:
QUOTED_STRING # QuotedStringValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import {
FunctionNameContext,
IncompleteAttributeExpressionMissingValueContext,
IncompletePlusTraversalExpressionContext,
IncompleteUpTraversalExpressionContext,
LeftParenTokenContext,
OrTokenContext,
PostDigitsWhitespaceContext,
PostDownwardTraversalWhitespaceContext,
PostLogicalOperatorWhitespaceContext,
PostNeighborTraversalWhitespaceContext,
Expand All @@ -30,7 +32,6 @@ import {

const DEFAULT_TEXT_CALLBACK = (value: string) => value;

// set to true for debug output if desired
const DEBUG = false;

export class SelectionAutoCompleteVisitor extends BaseSelectionVisitor {
Expand All @@ -43,7 +44,6 @@ export class SelectionAutoCompleteVisitor extends BaseSelectionVisitor {

public list: Array<Suggestion> = [];

// Replacement indices from the original code
public _startReplacementIndex: number;
public _stopReplacementIndex: number;

Expand Down Expand Up @@ -78,7 +78,6 @@ export class SelectionAutoCompleteVisitor extends BaseSelectionVisitor {
this._stopReplacementIndex = cursorIndex;
}

// Keep the same accessors and logging as before
set startReplacementIndex(newValue: number) {
if (DEBUG) {
console.log('Autocomplete suggestions being set by stack:', new Error());
Expand Down Expand Up @@ -255,6 +254,12 @@ export class SelectionAutoCompleteVisitor extends BaseSelectionVisitor {
}
}

public visitIncompleteUpTraversalExpression(_ctx: IncompleteUpTraversalExpressionContext) {
this.list.push(
this.createOperatorSuggestion({text: '+', type: 'up-traversal', displayText: '+'}),
);
}

public visitIncompletePlusTraversalExpression(ctx: IncompletePlusTraversalExpressionContext) {
if (
this.nodeIncludesCursor(ctx.postNeighborTraversalWhitespace()) &&
Expand Down Expand Up @@ -306,6 +311,18 @@ export class SelectionAutoCompleteVisitor extends BaseSelectionVisitor {
}
}

public visitPostDigitsWhitespace(ctx: PostDigitsWhitespaceContext) {
this.startReplacementIndex = ctx.start.startIndex;
this.stopReplacementIndex = ctx.stop!.stopIndex;
this.list.push(
this.createOperatorSuggestion({
text: '+',
type: 'up-traversal',
displayText: '+',
}),
);
}

public visitPostLogicalOperatorWhitespace(ctx: PostLogicalOperatorWhitespaceContext) {
if (!this.list.length) {
const isAfterNot = this.line.slice(0, this.cursorIndex).trim().toLowerCase().endsWith('not');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class SyntaxHighlightingVisitor

private addClass(ctx: ParserRuleContext, klass: string) {
const from = this.cm.posFromIndex(this.startOffset + ctx.start.startIndex);
const to = this.cm.posFromIndex(this.startOffset + ctx.stop!.stopIndex + 1);
const to = this.cm.posFromIndex(from.ch + ctx.text.length);
this.cm.markText(from, to, {className: klass});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1065,4 +1065,12 @@ describe('createAssetSelectionHint', () => {
to: 12,
});
});

it('suggests + after digits', () => {
expect(testAutocomplete('1234|')).toEqual({
from: 4,
list: [expect.objectContaining({text: '+'})],
to: 4,
});
});
});

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading