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
Draft
Show file tree
Hide file tree
Changes from 5 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
17 changes: 17 additions & 0 deletions rascal-vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,25 @@
{
"command": "rascalmpl.importMain",
"title": "Start Rascal Terminal and Import this module"
},
{
"command": "rascalmpl.copyLocation",
"title": "Copy Location of Selection to Clipboard"
}
],
"keybindings": [
{
"command": "rascalmpl.copyLocation",
"key": "shift+alt+l",
"mac": "shift+cmd+l"
}
],
"menus": {
"editor/context": [{
"command": "rascalmpl.copyLocation",
"group": "editor/context"
}]
},
"languages": [
{
"id": "rascalmpl",
Expand Down
35 changes: 35 additions & 0 deletions rascal-vscode-extension/src/RascalExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { RascalLanguageServer } from './lsp/RascalLanguageServer';
import { LanguageParameter, ParameterizedLanguageServer } from './lsp/ParameterizedLanguageServer';
import { RascalTerminalLinkProvider } from './RascalTerminalLinkProvider';
import { VSCodeUriResolverServer } from './fs/VSCodeURIResolver';
import {utf8to16Offset, utf8to16Length, utf8to16Column} from "./RascalTerminalLinkProvider";

export class RascalExtension implements vscode.Disposable {
private readonly vfsServer: VSCodeUriResolverServer;
Expand All @@ -51,6 +52,8 @@ export class RascalExtension implements vscode.Disposable {
this.registerMainRun();
this.registerImportModule();

this.registerCopyLocation();

vscode.window.registerTerminalLinkProvider(new RascalTerminalLinkProvider(this.rascal.rascalClient));
}

Expand Down Expand Up @@ -99,6 +102,38 @@ export class RascalExtension implements vscode.Disposable {
);
}


private registerCopyLocation() {
this.context.subscriptions.push(
vscode.commands.registerTextEditorCommand("rascalmpl.copyLocation", (text, _edit, moduleName) => {
const editor = vscode.window.activeTextEditor;
if (editor) {
const uri = editor.document.uri.toString();
const selection = editor.selection;

let offset = editor.document.offsetAt(selection.start);
let offsetUtf16 = utf8to16Offset(editor.document, offset);

let length = editor.document.getText(selection).length;
let lengthUtf16 = utf8to16Length(editor.document, offset, offset+length);

// the startline is not affected by UTF8 or UTF16, because line breaks are never UTF16
let startLine = selection.start.line;
let endLine = selection.end.line;

let startColumn = selection.start.character;
let startColumnUtf16 = utf8to16Column(editor.document, startLine, startColumn);
let endColumn = selection.end.character;
let endColumnUtf16 = utf8to16Column(editor.document, endLine, endColumn);

const location = `|${uri}|(${offsetUtf16},${lengthUtf16},<${startLine+1},${startColumnUtf16}>,<${endLine+1},${endColumnUtf16}>)`;

vscode.env.clipboard.writeText(location);
}
})
)
}

private startTerminal(uri: vscode.Uri | undefined, ...extraArgs: string[]) {
return vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
Expand Down
34 changes: 34 additions & 0 deletions rascal-vscode-extension/src/RascalTerminalLinkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,37 @@ function fixedOffsetLengthPositions(td: vscode.TextDocument, offset: number, len
}
return [offset, endOffset];
}

/**
* Convert the length of a selection from a UTF-8-based count (used by VS Code) to a UTF-16-based count (used by Rascal).
* Note how this function also works for calculating offsets by setting `begin` to 0.
* @param td the text document in which the selection was made
* @param endUtf8 the end position (UTF-8) of the selection
* @param beginUtf8 the begin of the selection (UTF-8)
* @returns length of the selection when counted as UTF-16.
*/
function utf8to16Conversation(text: string, beginUtf8: number, endUtf8: number): number {
let lengthUtf16 = endUtf8 - beginUtf8;

for (let i = beginUtf8; i < endUtf8-1; i++) {
const c = text.charCodeAt(i);
if (isHighSurrogate(c) && isLowSurrogate(text.charCodeAt(i + 1))) {
lengthUtf16--;
}
}

return lengthUtf16;
}

export function utf8to16Offset(td: vscode.TextDocument, offsetUtf8: number): number {
return utf8to16Conversation(td.getText(), 0, offsetUtf8);
}

export function utf8to16Length(td: vscode.TextDocument, beginUtf8: number, endUtf8: number): number {
return utf8to16Conversation(td.getText(), beginUtf8, endUtf8);
}

export function utf8to16Column(td: vscode.TextDocument, line: number, columnUtf8: number): number {
const fullLine = td.lineAt(line).text;
return utf8to16Conversation(fullLine, 0, columnUtf8);
}