Skip to content

Commit 74bf664

Browse files
author
Hofstetter Benjamin (extern)
committed
fix external query relative path
1 parent 5ce99c6 commit 74bf664

File tree

7 files changed

+120
-58
lines changed

7 files changed

+120
-58
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
All notable changes to the "vscode-sparql-notebook" extension will be documented in this file.
44

5+
## 0.0.29
6+
Fix: Store external query files relative to the notebook file. This is needed to make the notebook file portable.
7+
58
## 0.0.28
69

710
Minor: Shorten Blank Node IDs

package-lock.json

Lines changed: 45 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"repository": {
1010
"url": "https://github.com/zazuko/vscode-sparql-notebook.git"
1111
},
12-
"version": "0.0.28",
12+
"version": "0.0.29",
1313
"engines": {
1414
"vscode": "^1.77.0"
1515
},

samples/queries/z-external-query.rq

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#
1+
# Benjamin Hofstetter
22
# [endpoint=https://lindas.admin.ch/query]
33
#
44
PREFIX gont: <https://gont.ch/>

src/extension/commands/code-cell/add-query-from-file.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from "vscode";
2-
2+
import * as path from 'path';
33

44
export async function addQueryFromFile(cell: vscode.NotebookCell) {
55
const activeNotebook = cell.notebook;
@@ -19,14 +19,24 @@ export async function addQueryFromFile(cell: vscode.NotebookCell) {
1919

2020
const fileUri = await vscode.window.showOpenDialog(options);
2121
if (fileUri && fileUri.length > 0) {
22-
const filePath = fileUri[0].fsPath;
23-
const relativeFilePath = vscode.workspace.asRelativePath(filePath);
24-
22+
const sparqlFilePath = fileUri[0].fsPath;
23+
const activeNotebook = vscode.window.activeNotebookEditor?.notebook;
24+
if (!activeNotebook) {
25+
console.warn('No active notebook');
26+
return;
27+
}
2528
try {
26-
const fileContent = vscode.workspace.fs.readFile(fileUri[0]);
27-
const newCell = new vscode.NotebookCellData(vscode.NotebookCellKind.Code, `# from file ${relativeFilePath}\n${(await fileContent).toString()}`, 'sparql');
29+
const relativeSparqlFilePath = path.relative(path.dirname(activeNotebook.uri.fsPath), sparqlFilePath);
30+
const notebookFilePath = activeNotebook.uri.fsPath;
31+
const notebookFilename = path.basename(activeNotebook.uri.fsPath);
32+
const notebookPathWithoutFilename = notebookFilePath.replace(new RegExp(`${notebookFilename}$`), '');
33+
const fileContent = await vscode.workspace.fs.readFile(vscode.Uri.file(notebookPathWithoutFilename + relativeSparqlFilePath));
34+
35+
const newCell = new vscode.NotebookCellData(vscode.NotebookCellKind.Code, `# from file ${relativeSparqlFilePath}\n${(await fileContent).toString()}`, 'sparql');
36+
37+
console.log('store file path in metadata:', relativeSparqlFilePath);
2838
newCell.metadata = {
29-
file: filePath
39+
file: relativeSparqlFilePath
3040
};
3141
// Logic to add the notebook cell using the fileContent
3242
const notebookEdit = vscode.NotebookEdit.replaceCells(new vscode.NotebookRange(cell.index, cell.index + 1), [newCell]);
@@ -35,7 +45,7 @@ export async function addQueryFromFile(cell: vscode.NotebookCell) {
3545
vscode.workspace.applyEdit(edit);
3646
} catch (error) {
3747
// Handle file read error
38-
vscode.window.showErrorMessage(`Error reading file ${relativeFilePath}: ${error}`);
48+
vscode.window.showErrorMessage(`Error reading file ${sparqlFilePath}: ${error}`);
3949
console.error('Error reading file:', error);
4050
}
4151

src/extension/extension.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { addQueryFromFile } from "./commands/code-cell/add-query-from-file";
1414

1515
import { activateFormProvider } from "./connection-view/connection-view";
1616
import { createStoreFromFile } from "./commands/store-from-file/store-from-file";
17+
import path = require("path");
1718

1819
export const extensionId = "sparql-notebook";
1920
export const storageKey = `${extensionId}-connections`;
@@ -79,7 +80,56 @@ export function activate(context: vscode.ExtensionContext) {
7980
`${extensionId}.addQueryFromFile`,
8081
addQueryFromFile
8182
);
82-
}
83+
84+
85+
// load external notebook files
86+
// Execute code after a notebook is loaded
87+
// Register the onDidChangeNotebookDocument event
88+
context.subscriptions.push(vscode.workspace.onDidOpenNotebookDocument(notebookDocument => {
89+
// Check if the notebook is a SPARQL Notebook
90+
if (notebookDocument.notebookType !== extensionId) {
91+
return;
92+
}
93+
// reload the cells with external query file content
94+
// this have to be done here because we work with relative query file path
95+
// and the notebook path is not available in the deserializer
96+
const notebookPath = notebookDocument.uri.fsPath;
97+
const notebookDirectory = notebookPath.substring(0, notebookPath.lastIndexOf("/")) + '/';
98+
99+
// cells with a file metadata
100+
notebookDocument.getCells().filter(cell => cell.kind === vscode.NotebookCellKind.Code && cell.metadata.file).forEach(async cell => {
101+
const activeNotebook = cell.notebook;
102+
103+
if (activeNotebook) {
104+
const sparqlFilePath = cell.metadata.file;
105+
106+
try {
107+
let relativeSparqlFilePath = sparqlFilePath;
108+
if (path.isAbsolute(sparqlFilePath)) {
109+
relativeSparqlFilePath = path.relative(notebookDirectory, sparqlFilePath);
110+
}
111+
112+
const fileContent = await vscode.workspace.fs.readFile(vscode.Uri.file(notebookDirectory + relativeSparqlFilePath));
113+
const newCell = new vscode.NotebookCellData(vscode.NotebookCellKind.Code, `# from file ${relativeSparqlFilePath}\n${(await fileContent).toString()}`, 'sparql');
114+
115+
newCell.metadata = {
116+
file: relativeSparqlFilePath
117+
};
118+
// Logic to add the notebook cell using the fileContent
119+
const notebookEdit = vscode.NotebookEdit.replaceCells(new vscode.NotebookRange(cell.index, cell.index + 1), [newCell]);
120+
const edit = new vscode.WorkspaceEdit();
121+
edit.set(notebookDocument.uri, [notebookEdit]);
122+
vscode.workspace.applyEdit(edit);
123+
} catch (error) {
124+
// Handle file read error
125+
vscode.window.showErrorMessage(`Error reading file ${sparqlFilePath}: ${error}\n$Reset the file path for the cell. ${cell.index}`);
126+
console.error('Error reading file:', error);
127+
}
128+
}
129+
});
130+
}));
131+
132+
};
83133

84134
// this method is called when your extension is deactivated
85135
export function deactivate() { }

src/extension/file-io/sparql-notebook-serializer.class.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class SparqlNotebookSerializer implements vscode.NotebookSerializer {
4141
try {
4242
const fileContent = vscode.workspace.fs.readFile(filePath);
4343
cellData.value = `# from file ${relativeFilePath}\n${(await fileContent).toString()}`;
44+
// note the cell will be reloaded with the file content in the onDidOpenNotebookDocument
4445
return cellData;
4546
} catch (error) {
4647
// Handle file read error
@@ -97,8 +98,6 @@ export class SparqlNotebookSerializer implements vscode.NotebookSerializer {
9798
});
9899
}
99100

100-
101-
102101
return new TextEncoder()
103102
.encode(
104103
JSON.stringify(contents, null, 2)

0 commit comments

Comments
 (0)