Skip to content

Commit

Permalink
Fix keyboard shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
pehrs committed Jan 1, 2024
1 parent fd099ce commit d4c524b
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 70 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

All notable changes to the "vscode-vespa" extension will be documented in this file.


## [1.0.1]

- Minor bugfix for zipkin
- Fix keyboard shortcuts not clashing with vscode default shortcuts.

## [1.0.0]

- Initial release

6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,4 @@ This is not enabled as the features it gives, at the moment, is limited.

## Release Notes

### 1.0.0

Initial release

---
Please read the [CHANGELOG](CHANGELOG.md)
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "VSCode part of a language server",
"author": "Matti Pehrs",
"license": "MIT",
"version": "1.0.0",
"version": "1.0.1",
"publisher": "pehrs.com",
"engines": {
"vscode": "^1.75.0"
Expand Down
94 changes: 60 additions & 34 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export function activate(context: ExtensionContext) {
registerRunQueryCommand(context);
registerFormatterCommand(context);

registerYqlFormatter(context);

// NOT Needed yet as we are not saving the result state anywhere
// Results panel view
// if (vscode.window.registerWebviewPanelSerializer) {
Expand Down Expand Up @@ -231,11 +233,13 @@ function registerRunQueryCommand(context: ExtensionContext) {

const editor = vscode.window.activeTextEditor;

outputChannel.appendLine(`file: ${editor.document.fileName}`);

const workbenchConfig = vscode.workspace.getConfiguration('vespaYql');
const queryEndpoint: string = workbenchConfig.get('queryEndpoint');
const queryTimeout: string = workbenchConfig.get('queryTimeout');

if (editor) {
if (editor && editor.document.fileName.endsWith(".yql")) {
const document = editor.document;
// Get the document text
const yql = document.getText();
Expand All @@ -247,38 +251,60 @@ function registerRunQueryCommand(context: ExtensionContext) {
}


function registerFormatterCommand(context: ExtensionContext) {
const runYqlCommand = vscode.commands.registerCommand('vscode-vespa.format', () => {
function registerYqlFormatter(context: ExtensionContext) {
vscode.languages.registerDocumentFormattingEditProvider('yql', {
provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] {

const editor = vscode.window.activeTextEditor;
formatYql();

// const firstLine = document.lineAt(0);
// if (firstLine.text !== '42') {
// return [vscode.TextEdit.insert(firstLine.range.start, '42\n')];
// }
return [];
}
});
}

if (editor) {
try {
const document = editor.document;
// Get the document text
const yql = document.getText();

const jsonObj = JSON.parse(yql);
const newYql = JSON.stringify(jsonObj, null, 2);

const start = new vscode.Position(0, 0);
const lastLine = document.lineCount - 1;
const end = new vscode.Position(lastLine, document.lineAt(lastLine).text.length);
const allText = new vscode.Range(start, end);

const edit = new vscode.WorkspaceEdit();
edit.replace(document.uri, allText, newYql);
// edit.insert(document.uri, firstLine.range.start, '42\n');

return vscode.workspace.applyEdit(edit);
} catch (e) {
if (typeof e === "string") {
showError(e);
} else if (e instanceof Error) {
showError(e.message);
}

function formatYql() {
const editor = vscode.window.activeTextEditor;

outputChannel.appendLine(`format ${editor.document.fileName}`);

if (editor && editor.document.fileName.endsWith(".yql")) {
try {
const document = editor.document;
// Get the document text
const yql = document.getText();

const jsonObj = JSON.parse(yql);
const newYql = JSON.stringify(jsonObj, null, 2);

const start = new vscode.Position(0, 0);
const lastLine = document.lineCount - 1;
const end = new vscode.Position(lastLine, document.lineAt(lastLine).text.length);
const allText = new vscode.Range(start, end);

const edit = new vscode.WorkspaceEdit();
edit.replace(document.uri, allText, newYql);
// edit.insert(document.uri, firstLine.range.start, '42\n');

return vscode.workspace.applyEdit(edit);
} catch (e) {
if (typeof e === "string") {
showError(e);
} else if (e instanceof Error) {
showError(e.message);
}
}
}
}

function registerFormatterCommand(context: ExtensionContext) {
const runYqlCommand = vscode.commands.registerCommand('vscode-vespa.format', () => {

formatYql();
});
context.subscriptions.push(runYqlCommand);
}
Expand Down Expand Up @@ -709,8 +735,8 @@ class YqlResultsPanel {
// TRACE
if (this.zipkinLink !== undefined) {
result += `<div class="tab"><p>`;
result += `<h2><a href="${this.zipkinLink}">Open in browser...</a></h2>`;
result += `<div><iframe width="100%" height=1024px" src="${this.zipkinLink}" title="Zipkin Trace"></iframe></div>`;
result += `<h2><a href="${this.zipkinLink}">Open in browser...</a></h2>`;
result += `<div><iframe width="100%" height=1024px" src="${this.zipkinLink}" title="Zipkin Trace"></iframe></div>`;
result += `</p></div>`;
}

Expand Down Expand Up @@ -745,12 +771,12 @@ class YqlResultsPanel {

// Enable the iframe to load the zipkin url
let cspUrl = "http://127.0.0.1:9411 http://localhost:9411";
if(this.zipkinLink !== undefined) {
if (this.zipkinLink !== undefined) {
const url = new URL(this.zipkinLink);
cspUrl = `${url.protocol}//${url.host}`;
if(url.hostname === "localhost") {
if (url.hostname === "localhost") {
cspUrl = `${url.protocol}//127.0.0.1:${url.port} ${cspUrl}`;
}
}
}
// outputChannel.appendLine(`cspUrl: ${cspUrl}`);

Expand Down
3 changes: 3 additions & 0 deletions client/testFixture/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"json": "sample"
}
32 changes: 16 additions & 16 deletions client/testFixture/test.yql
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"yql": "select title, url from msmarco where true limit 100",
"offset": 5,
"ranking": {
"matchPhase": {
"ascending": true,
"maxHits": 15
}
},
"presentation" : {
"bolding": false,
"format": "json"
},
"trace": {
"level": 4,
"timestamps": true
}
"yql": "select title, url from msmarco where true limit 100",
"offset": 5,
"ranking": {
"matchPhase": {
"ascending": true,
"maxHits": 15
}
},
"presentation": {
"bolding": false,
"format": "json"
},
"trace": {
"level": 4,
"timestamps": true
}
}
15 changes: 3 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-vespa",
"displayName": "vscode-vespa",
"description": "Vespa AI Extension",
"version": "1.0.0",
"version": "1.0.1",
"publisher": "pehrs-com",
"license": "SEE LICENSE IN LICENSE",
"icon": "media/vespa.png",
Expand Down Expand Up @@ -41,13 +41,9 @@
"keybindings": [
{
"command": "vscode-vespa.runQuery",
"when": "editorLangId == yql",
"key": "ctrl+enter",
"mac": "cmd+enter"
},
{
"command": "vscode-vespa.format",
"key": "ctrl+shift+i",
"mac": "cmd+shift-i"
}
],
"menus": {
Expand All @@ -68,11 +64,6 @@
"when": "resourceLangId == yql",
"command": "vscode-vespa.runQuery",
"group": "navigation"
},
{
"when": "resourceLangId == yql",
"command": "vscode-vespa.format",
"group": "navigation"
}
]
},
Expand Down Expand Up @@ -155,4 +146,4 @@
"dependencies": {
"node-fetch": "^3.3.2"
}
}
}
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vscode-vespa-server",
"description": "Example implementation of a language server in node.",
"version": "1.0.0",
"version": "1.0.1",
"author": "Matti Pehrs",
"license": "MIT",
"engines": {
Expand Down

0 comments on commit d4c524b

Please sign in to comment.