Skip to content

Commit

Permalink
use uri format for notebook cells that uses fragments, #97881
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed May 15, 2020
1 parent 0a72cff commit db809fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
20 changes: 11 additions & 9 deletions src/vs/workbench/contrib/notebook/common/notebookCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { IEditorModel } from 'vs/platform/editor/common/editor';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { GlobPattern } from 'vs/workbench/api/common/extHost.protocol';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Schemas } from 'vs/base/common/network';

export enum CellKind {
Markdown = 1,
Expand Down Expand Up @@ -330,25 +331,26 @@ export namespace CellUri {

export function generate(notebook: URI, handle: number): URI {
return notebook.with({
path: `${notebook.path}, cell ${handle + 1}`,
query: JSON.stringify({ cell: handle, notebook: notebook.toString() }),
scheme,
fragment: `${handle}${notebook.scheme !== Schemas.file ? notebook.scheme : ''}`
});
}

export function parse(cell: URI): { notebook: URI, handle: number } | undefined {
if (cell.scheme !== scheme) {
return undefined;
}
try {
const data = <{ cell: number, notebook: string }>JSON.parse(cell.query);
return {
handle: data.cell,
notebook: URI.parse(data.notebook)
};
} catch {
const handle = parseInt(cell.fragment);
if (isNaN(handle)) {
return undefined;
}
return {
handle,
notebook: cell.with({
scheme: cell.fragment.substr(handle.toString().length) || Schemas.file,
fragment: null
})
};
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/vs/workbench/contrib/notebook/test/notebookCommon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,19 @@ suite('NotebookCommon', () => {

suite('CellUri', function () {

test('parse, generate', function () {
test('parse, generate (file-scheme)', function () {

const nb = URI.parse('foo:///bar/følder/file.nb');
const id = 17;

const data = CellUri.generate(nb, id);
const actual = CellUri.parse(data);
assert.ok(Boolean(actual));
assert.equal(actual?.handle, id);
assert.equal(actual?.notebook.toString(), nb.toString());
});

test('parse, generate (foo-scheme)', function () {

const nb = URI.parse('foo:///bar/følder/file.nb');
const id = 17;
Expand Down

0 comments on commit db809fa

Please sign in to comment.