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

Add command to create rascal location from selection #283

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

linuswagner
Copy link

@linuswagner linuswagner commented Aug 21, 2023

This PR adds a command that allows users to get a Rascal location for their selection put into their clipboard.

If you review this, you should check the following things:

  • does the shortcut for Mac make sense?
  • can the code for the command stay where it is, or should it be refactored in a separate method so it doesn't clutter the registerXXX method?
  • is it fine to add the command for every type of file?
  • does it need tests? Currently, there are none
  • is the command in the context menu at a position where it makes sense?

Limitations:

  • only allows to create an absolute path (i.e. starting with "file://") at the moment
  • breaks without warning, if user has multiple selections -> takes the first one

@linuswagner
Copy link
Author

@jurgenvinju can you give this a look?

@DavyLandman
Copy link
Member

DavyLandman commented Aug 22, 2023 via email

@jurgenvinju
Copy link
Member

@linuswagner @DavyLandman let's fix the offsets together before we merge the PR. I'd also like to add a feature where a list of tagged locations can be collected in a file (configured by the user). That way we help many users that need to manually get some ground truth about a code corpus.

@linuswagner
Copy link
Author

linuswagner commented Aug 22, 2023

@linuswagner @DavyLandman let's fix the offsets together before we merge the PR. I'd also like to add a feature where a list of tagged locations can be collected in a file (configured by the user). That way we help many users that need to manually get some ground truth about a code corpus.

Sure. Keep in mind that I've forked the repo for this PR, so you either fork the fork and create a PR on mine or we move my commit to a branch here and close this PR.
Nvm, as Davy pointed out, you can directly commit on my main

@linuswagner linuswagner marked this pull request as draft August 22, 2023 11:50
@DavyLandman
Copy link
Member

DavyLandman commented Aug 22, 2023 via email

Copy link
Member

@DavyLandman DavyLandman left a comment

Choose a reason for hiding this comment

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

Looks good @linuswagner!

I've added some comments on how to get this to be compatible with more use cases of rascal. Do you have some time to pick that up?

rascal-vscode-extension/src/RascalExtension.ts Outdated Show resolved Hide resolved
rascal-vscode-extension/src/RascalExtension.ts Outdated Show resolved Hide resolved
@linuswagner
Copy link
Author

@DavyLandman thanks for the feedback. I'll be taking a look on Friday/the weekend and see what I can do.

@DavyLandman
Copy link
Member

@DavyLandman thanks for the feedback. I'll be taking a look on Friday/the weekend and see what I can do.

awesome 👍🏼

@linuswagner
Copy link
Author

@DavyLandman took a bit longer than expected, but UTF16 characters now work.
I have essentially written the inverse of the functions in RascalTerminalLinkProvider.
They currently are also in the same file.

If you give me a rough description of the interface you want for UTF8 <-> UTF16 conversion (including where to place it), I can also do the refactoring you further up.

Copy link
Member

@DavyLandman DavyLandman left a comment

Choose a reason for hiding this comment

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

Hi @linuswagner, Great work! Thank you for taking the time to pick this up.

With regards to your question. Maybe for now make a util folder? and put the PositionConverter class in there?

rascal-vscode-extension/src/RascalExtension.ts Outdated Show resolved Hide resolved
rascal-vscode-extension/src/RascalTerminalLinkProvider.ts Outdated Show resolved Hide resolved
@DavyLandman
Copy link
Member

also @linuswagner the build is failing due to some of es-lint messages, you can run it by npm run lint and try and fix those.

@linuswagner
Copy link
Author

@DavyLandman All done. Can you also please check vsCodeToRascalRange. I'm a bit confused when it comes to the lines when they should be 1-based and when 0-based

Copy link
Member

@DavyLandman DavyLandman left a comment

Choose a reason for hiding this comment

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

hi @linuswagner thanks! much imporved 👍🏼

Since you asked me to carefully review the calc functions I did. I think some stuff has to be at least aligned with the documentation of the function. It might be that I misunderstood the code, feel free to point that out and explain what is happening.

* @returns the column as understood by VS Code.
*/
static rascalToVSCodeColumn(td: vscode.TextDocument, line: number, rascalColumn: number): number {
const fullLine = td.lineAt(line).text;
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be line - 1? (and maybe named rascalLine?) Or should we document that we assume the caller already has converted the line number to vs code?

i++;
}
}
return [offset, endOffset];
Copy link
Member

Choose a reason for hiding this comment

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

this has a bug, as the endOffset is not the length. Maybe should it be endOffset - offset ? or we should make it clear it returns 2 positions.

*/
static vsCodeToRascalColumn(td: vscode.TextDocument, line: number, columnVSCode: number): number {
const fullLine = td.lineAt(line).text;
let lengthRascal = columnVSCode;
Copy link
Member

Choose a reason for hiding this comment

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

NP: should it be called columnRascal?


for (let i = 0; i < columnVSCode - 1; i++) {
const c = fullLine.charCodeAt(i);
if (PositionConverter.isHighSurrogate(c) && PositionConverter.isLowSurrogate(fullLine.charCodeAt(i + 1))) {
Copy link
Member

Choose a reason for hiding this comment

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

will throw exception if (i + 1) == length.

const fullLine = td.lineAt(line).text;
let lengthRascal = columnVSCode;

for (let i = 0; i < columnVSCode - 1; i++) {
Copy link
Member

Choose a reason for hiding this comment

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

we should make sure that columnVSCode is not more than fullLine.length()

static vsCodeToRascalOffsetLength(td: vscode.TextDocument, offset: number, length: number): [number, number] {
const fullText = td.getText();
const endOffset = offset + length;
let newEndOffset = endOffset;
Copy link
Member

Choose a reason for hiding this comment

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

should be called newLength? and just be newLength = length?

i++; // the following letter is known to be the low surrogate -> we can skip it
}
}
return [offset, newEndOffset];
Copy link
Member

Choose a reason for hiding this comment

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

should not return 2 positions, but 1 offset, and one length.

let newEndOffset = endOffset;
for (let i = 0; i < endOffset - 1; i++) {
const c = fullText.charCodeAt(i);
if (PositionConverter.isHighSurrogate(c) && PositionConverter.isLowSurrogate(fullText.charCodeAt(i + 1))) {
Copy link
Member

Choose a reason for hiding this comment

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

also, same bug, i+1 might throw exception in case of malformed string.

@jurgenvinju
Copy link
Member

This is a lovely feature to have; I really miss it (since we had it in the Eclipse version). Thanks both for putting your time into this.

@DavyLandman DavyLandman added the enhancement New feature or request label Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants