Skip to content

Commit

Permalink
[Selection auto complete] Suggest "+" after digits. (#28121)
Browse files Browse the repository at this point in the history
## Summary & Motivation
The only possible continuation after typing digits is a `+` so lets
suggest that.

## How I Tested These Changes

jest + manual testing
<img width="1231" alt="Screenshot 2025-02-28 at 9 22 17 AM"
src="https://github.com/user-attachments/assets/620d767e-f2b9-4223-a73a-ba935faa09af"
/>
  • Loading branch information
salazarm authored Feb 28, 2025
1 parent 53ccd93 commit ef0670e
Show file tree
Hide file tree
Showing 8 changed files with 484 additions and 265 deletions.
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

1 comment on commit ef0670e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for dagit-core-storybook ready!

✅ Preview
https://dagit-core-storybook-12m1j3yzj-elementl.vercel.app

Built with commit ef0670e.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.