-
Notifications
You must be signed in to change notification settings - Fork 273
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
fix: use virtual caret in tests #722
Conversation
if (typeof position === 'undefined') { | ||
position = content.indexOf('|:|'); | ||
content = content.replace('|:|', ''); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the critical change
If the position
is not specified, we look inside the content
to derive the position
using the index of the |:|
token
const content = '|:|'; | ||
const completion = parseSetup(content); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scenario 1: position === 0
const content = 'na|:|'; | ||
const completion = parseSetup(content); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scenario 2: position === <postfix>
const content = 'nam|:|e'; | ||
const completion = await parseSetup(content); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scenario 3: position === <infix>
const content = 'scripts: |:|'; | ||
const completion = parseSetup(content); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scenario 4: position === <length>
@@ -111,8 +111,9 @@ export class YamlCompletion { | |||
setKubernetesParserOption(doc.documents, isKubernetes); | |||
|
|||
const offset = document.offsetAt(position); | |||
const text = document.getText(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perf optimization: just call getText()
once
This is so cool. First on my list for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks for the contribution
Thanks @gorkem , @msivasubramaniaan |
@grant-d love this, counting chars was an adventure :) |
Yes - my plan was to introduce the concept in a bite-sized PR, then once I got buy-in, I will spread the love elsewhere |
What does this PR do?
It improves core test infrastructure:
While helping to fix several autocomplete bugs (eg #724) in the language server, it was difficult to understand where the "cursor" was placed in sample
content
within thetests
.The existing code uses the following method signature, whereby the caller has to do the math on the
position
parameter to establish where the caret is placed:While this works, it is difficult to maintain since it's not easy to understand the test by just looking at the
content
(& without doing math)This PR introduces the notion of a virtual caret, which is specified in the
content
itself.The token
|:|
is chosen to be easily noticed, and not conflict with 'real' yamlWhat issues does this PR fix or reference?
Tests are simpler to write and maintain since the cursor's position is explicit in the content.
Is it tested? How?
The changed method is back-compatible, so all existing tests continue to pass.
However, I have updated one example
test
of each interesting scenario:|:|abc
abc|:|
ab|:|c
If this PR is approved, I will update the other tests accordingly. I updated just a few tests for now to keep the noise down and the changes easy to identify.
cc @p-spacek