From e89b8f99170a7fb1bc6c16393e87d37313fcf85f Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 14 Sep 2020 13:51:11 +0100 Subject: [PATCH 01/13] warning --- src/test/suite/extension.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index 5d3e49ab..a77e028a 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -47,7 +47,7 @@ suite('Test commands', () => { suite('copy-github-permalink.copy', () => { test('Display copied information and put the link to clipboard', async () => { - const infoStub = sandbox.stub(vscode.window, 'showInformationMessage') as sinon.SinonStub<[string, any?], Thenable>; + const infoStub = sandbox.stub(vscode.window, 'showInformationMessage') as unknown as sinon.SinonStub<[string], Thenable>; configureBranch(sandbox, 'HEAD'); mockGit(); @@ -59,7 +59,7 @@ suite('Test commands', () => { }); test('Git remote is HTTP', async () => { - const infoStub = sandbox.stub(vscode.window, 'showInformationMessage') as sinon.SinonStub<[string, any?], Thenable>; + const infoStub = sandbox.stub(vscode.window, 'showInformationMessage') as unknown as sinon.SinonStub<[string], Thenable>; configureBranch(sandbox, 'HEAD'); mockGit('http://github.com/owner/name.git'); @@ -71,7 +71,7 @@ suite('Test commands', () => { }); test('Git remote is HTTPS', async () => { - const infoStub = sandbox.stub(vscode.window, 'showInformationMessage') as sinon.SinonStub<[string, any?], Thenable>; + const infoStub = sandbox.stub(vscode.window, 'showInformationMessage') as unknown as sinon.SinonStub<[string], Thenable>; configureBranch(sandbox, 'HEAD'); mockGit('https://github.com/owner/name.git'); @@ -83,7 +83,7 @@ suite('Test commands', () => { }); test('Display copied information and put the link of all lines to clipboard', async () => { - const infoStub = sandbox.stub(vscode.window, 'showInformationMessage') as sinon.SinonStub<[string, any?], Thenable>; + const infoStub = sandbox.stub(vscode.window, 'showInformationMessage') as unknown as sinon.SinonStub<[string], Thenable>; configureBranch(sandbox, 'HEAD'); mockGit(); @@ -96,7 +96,7 @@ suite('Test commands', () => { }); test('Select another branch', async () => { - const infoStub = sandbox.stub(vscode.window, 'showInformationMessage') as sinon.SinonStub<[string, any?], Thenable>; + const infoStub = sandbox.stub(vscode.window, 'showInformationMessage') as unknown as sinon.SinonStub<[string], Thenable>; configureBranch(sandbox, 'origin/master'); mockGit(); @@ -108,7 +108,7 @@ suite('Test commands', () => { }); test('Cannot get git information', async () => { - const warningStub = sandbox.stub(vscode.window, 'showWarningMessage') as sinon.SinonStub<[string, any?], Thenable>; + const warningStub = sandbox.stub(vscode.window, 'showWarningMessage') as unknown as sinon.SinonStub<[string], Thenable>; configureBranch(sandbox, 'origin/master'); mockGit(''); From df10f54e91f0e16d14ba85e53fbdbfdba4cd065e Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 14 Sep 2020 13:52:05 +0100 Subject: [PATCH 02/13] update --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0d4cc3bd..8b0f5725 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "url": "https://github.com/tejanium/vscode-copy-github-permalink" }, "engines": { - "vscode": "^1.47.0" + "vscode": "^1.49.0" }, "categories": [ "Other" From 3c2fec9bee0aed7977541b6b9e3eba0c6e15e556 Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 14 Sep 2020 14:30:45 +0100 Subject: [PATCH 03/13] view --- package.json | 6 +++++- src/extension.ts | 28 ++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 8b0f5725..d3402f87 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "Other" ], "activationEvents": [ - "onCommand:copy-github-permalink.copy" + "onCommand:copy-github-permalink.copy", + "onCommand:copy-github-permalink.view" ], "main": "./dist/extension", "contributes": { @@ -29,6 +30,9 @@ { "command": "copy-github-permalink.copy", "title": "Github Permalink: Copy" + }, { + "command": "copy-github-permalink.view", + "title": "Github Permalink: View" } ], "configuration": { diff --git a/src/extension.ts b/src/extension.ts index 92796eca..43d02ace 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,7 +2,7 @@ import * as vscode from 'vscode'; import { Permalink } from './model/permalink'; export function activate(context: vscode.ExtensionContext) { - let disposable = vscode.commands.registerCommand('copy-github-permalink.copy', async () => { + let copy = vscode.commands.registerCommand('copy-github-permalink.copy', async () => { const editor = vscode.window.activeTextEditor; if (editor && editor.document.uri.scheme === 'file') { @@ -20,7 +20,31 @@ export function activate(context: vscode.ExtensionContext) { } }); - context.subscriptions.push(disposable); + context.subscriptions.push(copy); + + let view = vscode.commands.registerCommand('copy-github-permalink.view', async () => { + const input = await vscode.window.showInputBox() + const [blob, position] = input?.split("blob").pop()?.split("/").slice(2).join("/").split("#") || []; + + if (blob) { + const uri = await vscode.workspace.findFiles(`**/${blob}`); + const textDocument = await vscode.workspace.openTextDocument(uri[0]); + const opts = { preview: false } + + if (position) { + const [start, end] = position.replace(/L/g, "").split("-"); + const startPosition = new vscode.Position(parseInt(start) - 1, 0); + const endPosition = new vscode.Position(parseInt(end), 0); + const range = new vscode.Range(startPosition, endPosition); + + Object.assign(opts, { selection: range }) + } + + vscode.window.showTextDocument(textDocument, opts) + } + }); + + context.subscriptions.push(view); } function getBranch(): string { From 8e86025d27b039124bb183b1a2cf3948a7e4a586 Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 14 Sep 2020 14:57:08 +0100 Subject: [PATCH 04/13] tests --- src/test/suite/extension.test.ts | 46 ++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index a77e028a..b043f11b 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -28,12 +28,18 @@ function configureBranch(sandbox: sinon.SinonSandbox, branch: string): void { } as vscode.WorkspaceConfiguration); }; +async function getFixtureFile(path: string): Promise { + const root = vscode.workspace.workspaceFolders![0].uri.fsPath; + + return await vscode.workspace.openTextDocument(`${root}/${path}`); +} + suite('Test commands', () => { let sandbox: sinon.SinonSandbox; before(async () => { - const root = vscode.workspace.workspaceFolders![0].uri.fsPath; - const document = await vscode.workspace.openTextDocument(`${root}/test/fixtures/file.txt`); + const document = await getFixtureFile('test/fixtures/file.txt'); + await vscode.window.showTextDocument(document); }); @@ -118,4 +124,40 @@ suite('Test commands', () => { sandbox.assert.calledWith(warningStub, 'Could not get Git info, please try a little later'); }); }); + + suite('copy-github-permalink.view', () => { + test('Open the file', async () => { + const showDocumentStub = sandbox.stub(vscode.window, 'showTextDocument') as unknown as sinon.SinonStub<[vscode.TextDocument, vscode.TextDocumentShowOptions]>; + + sandbox.stub(vscode.window, 'showInputBox').resolves('https://github.com/owner/name/blob/sha1234567890/test/fixtures/file.txt#L1-L3'); + + await vscode.commands.executeCommand('copy-github-permalink.view'); + + const document = await getFixtureFile('test/fixtures/file.txt'); + const range = new vscode.Range(new vscode.Position(0, 0), new vscode.Position(3, 0)); + sandbox.assert.calledWith(showDocumentStub, document, { preview: false, selection: range }); + }); + + test('Open the file even without sha', async () => { + const showDocumentStub = sandbox.stub(vscode.window, 'showTextDocument') as unknown as sinon.SinonStub<[vscode.TextDocument, vscode.TextDocumentShowOptions]>; + + sandbox.stub(vscode.window, 'showInputBox').resolves('https://github.com/owner/name/blob/master/test/fixtures/file.txt'); + + await vscode.commands.executeCommand('copy-github-permalink.view'); + + const document = await getFixtureFile('test/fixtures/file.txt'); + sandbox.assert.calledWith(showDocumentStub, document, { preview: false }); + }); + + test('Open the file even without line', async () => { + const showDocumentStub = sandbox.stub(vscode.window, 'showTextDocument') as unknown as sinon.SinonStub<[vscode.TextDocument, vscode.TextDocumentShowOptions]>; + + sandbox.stub(vscode.window, 'showInputBox').resolves('https://github.com/owner/name/blob/sha1234567890/test/fixtures/file.txt'); + + await vscode.commands.executeCommand('copy-github-permalink.view'); + + const document = await getFixtureFile('test/fixtures/file.txt'); + sandbox.assert.calledWith(showDocumentStub, document, { preview: false }); + }); + }); }); From 16feb2d21b456652c86bca2dc6fb90ea4c31fe5a Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 14 Sep 2020 14:58:55 +0100 Subject: [PATCH 05/13] lint --- package.json | 2 +- src/extension.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index d3402f87..8193c238 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "vscode:prepublish": "webpack --mode production", "compile": "webpack --mode development", "test-compile": "tsc -p ./", - "lint": "eslint src --ext ts", + "lint": "eslint src --ext ts --fix", "watch": "webpack --mode development --watch --info-verbosity verbose", "pretest": "yarn --frozen-lockfile && yarn compile && yarn test-compile && yarn lint", "test": "node ./out/test/runTest.js", diff --git a/src/extension.ts b/src/extension.ts index 43d02ace..9c6c6f6d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -23,13 +23,13 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(copy); let view = vscode.commands.registerCommand('copy-github-permalink.view', async () => { - const input = await vscode.window.showInputBox() + const input = await vscode.window.showInputBox(); const [blob, position] = input?.split("blob").pop()?.split("/").slice(2).join("/").split("#") || []; if (blob) { const uri = await vscode.workspace.findFiles(`**/${blob}`); const textDocument = await vscode.workspace.openTextDocument(uri[0]); - const opts = { preview: false } + const opts = { preview: false }; if (position) { const [start, end] = position.replace(/L/g, "").split("-"); @@ -37,10 +37,10 @@ export function activate(context: vscode.ExtensionContext) { const endPosition = new vscode.Position(parseInt(end), 0); const range = new vscode.Range(startPosition, endPosition); - Object.assign(opts, { selection: range }) + Object.assign(opts, { selection: range }); } - vscode.window.showTextDocument(textDocument, opts) + vscode.window.showTextDocument(textDocument, opts); } }); From 6f4794ff251d386c8a1b675c7ca0fad1221134d3 Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 14 Sep 2020 15:38:44 +0100 Subject: [PATCH 06/13] class --- src/extension.ts | 19 ++++--------------- src/model/document.ts | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 src/model/document.ts diff --git a/src/extension.ts b/src/extension.ts index 9c6c6f6d..5071d411 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,4 +1,5 @@ import * as vscode from 'vscode'; +import { Document } from './model/document'; import { Permalink } from './model/permalink'; export function activate(context: vscode.ExtensionContext) { @@ -24,23 +25,11 @@ export function activate(context: vscode.ExtensionContext) { let view = vscode.commands.registerCommand('copy-github-permalink.view', async () => { const input = await vscode.window.showInputBox(); - const [blob, position] = input?.split("blob").pop()?.split("/").slice(2).join("/").split("#") || []; - if (blob) { - const uri = await vscode.workspace.findFiles(`**/${blob}`); - const textDocument = await vscode.workspace.openTextDocument(uri[0]); - const opts = { preview: false }; + if (input) { + const document = new Document(input); - if (position) { - const [start, end] = position.replace(/L/g, "").split("-"); - const startPosition = new vscode.Position(parseInt(start) - 1, 0); - const endPosition = new vscode.Position(parseInt(end), 0); - const range = new vscode.Range(startPosition, endPosition); - - Object.assign(opts, { selection: range }); - } - - vscode.window.showTextDocument(textDocument, opts); + vscode.window.showTextDocument(await document.document(), document.showOptions); } }); diff --git a/src/model/document.ts b/src/model/document.ts new file mode 100644 index 00000000..bd5e66a8 --- /dev/null +++ b/src/model/document.ts @@ -0,0 +1,44 @@ +import * as vscode from 'vscode'; + +export class Document { + private blob: string | undefined; + private position: string | undefined; + + constructor(private permalink: string) { + [this.blob, this.position] = this.relativePathWithLines.split('#') || []; + } + + async document(): Promise { + const root = vscode.workspace.workspaceFolders![0].uri.fsPath; + + return await vscode.workspace.openTextDocument(`${root}/${this.blob}`); + } + + get showOptions(): vscode.TextDocumentShowOptions { + const opts = { preview: false }; + + if (this.position) { + Object.assign(opts, { selection: this.range }); + } + + return opts; + } + + private get relativePathWithLines(): string { + return this.permalink.split('blob').pop()?.split('/').slice(2).join('/') || ''; + } + + private get range(): vscode.Range | undefined { + const [start, end] = this.position?.split("-") || []; + const startPosition = this.getPosition(start, -1); + const endPosition = this.getPosition(end); + + return new vscode.Range(startPosition, endPosition); + } + + private getPosition(raw: string | undefined, offset: number = 0) { + const safeRaw = raw?.replace(/\D/g, '') || '0'; + + return new vscode.Position(parseInt(safeRaw) + offset, 0); + } +} From e002728eab3e48da0e7bea42f8504d9220e55011 Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 14 Sep 2020 15:39:15 +0100 Subject: [PATCH 07/13] path --- src/model/document.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/model/document.ts b/src/model/document.ts index bd5e66a8..b1e4ae5e 100644 --- a/src/model/document.ts +++ b/src/model/document.ts @@ -1,17 +1,17 @@ import * as vscode from 'vscode'; export class Document { - private blob: string | undefined; + private path: string | undefined; private position: string | undefined; constructor(private permalink: string) { - [this.blob, this.position] = this.relativePathWithLines.split('#') || []; + [this.path, this.position] = this.relativePathWithLines.split('#') || []; } async document(): Promise { const root = vscode.workspace.workspaceFolders![0].uri.fsPath; - return await vscode.workspace.openTextDocument(`${root}/${this.blob}`); + return await vscode.workspace.openTextDocument(`${root}/${this.path}`); } get showOptions(): vscode.TextDocumentShowOptions { From 0890dbfbb36ef772863624beacbd0f58ae6ab0f3 Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 21 Sep 2020 09:44:31 +0100 Subject: [PATCH 08/13] refactor --- src/extension.ts | 7 ++++++- src/model/document.ts | 22 ++++++++-------------- src/test/suite/extension.test.ts | 11 +++++++++++ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 5071d411..8c45fcb7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -27,9 +27,14 @@ export function activate(context: vscode.ExtensionContext) { const input = await vscode.window.showInputBox(); if (input) { + const showOpts = { preview: false }; const document = new Document(input); - vscode.window.showTextDocument(await document.document(), document.showOptions); + if (document.range) { + Object.assign(showOpts, { selection: document.range }); + } + + vscode.window.showTextDocument(await document.document(), showOpts); } }); diff --git a/src/model/document.ts b/src/model/document.ts index b1e4ae5e..17f4f7c9 100644 --- a/src/model/document.ts +++ b/src/model/document.ts @@ -14,28 +14,22 @@ export class Document { return await vscode.workspace.openTextDocument(`${root}/${this.path}`); } - get showOptions(): vscode.TextDocumentShowOptions { - const opts = { preview: false }; - - if (this.position) { - Object.assign(opts, { selection: this.range }); + get range(): vscode.Range | undefined { + if (!this.position) { + return; } - return opts; - } - - private get relativePathWithLines(): string { - return this.permalink.split('blob').pop()?.split('/').slice(2).join('/') || ''; - } - - private get range(): vscode.Range | undefined { - const [start, end] = this.position?.split("-") || []; + const [start, end] = this.position.split("-") || []; const startPosition = this.getPosition(start, -1); const endPosition = this.getPosition(end); return new vscode.Range(startPosition, endPosition); } + private get relativePathWithLines(): string { + return this.permalink.split('blob').pop()?.split('/').slice(2).join('/') || ''; + } + private getPosition(raw: string | undefined, offset: number = 0) { const safeRaw = raw?.replace(/\D/g, '') || '0'; diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index b043f11b..841f058e 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -159,5 +159,16 @@ suite('Test commands', () => { const document = await getFixtureFile('test/fixtures/file.txt'); sandbox.assert.calledWith(showDocumentStub, document, { preview: false }); }); + + test('Open the file even with missing line', async () => { + const showDocumentStub = sandbox.stub(vscode.window, 'showTextDocument') as unknown as sinon.SinonStub<[vscode.TextDocument, vscode.TextDocumentShowOptions]>; + + sandbox.stub(vscode.window, 'showInputBox').resolves('https://github.com/owner/name/blob/sha1234567890/test/fixtures/file.txt#'); + + await vscode.commands.executeCommand('copy-github-permalink.view'); + + const document = await getFixtureFile('test/fixtures/file.txt'); + sandbox.assert.calledWith(showDocumentStub, document, { preview: false }); + }); }); }); From 2f0287987bdc887786905ea18d6b2afba4a71eb2 Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 21 Sep 2020 09:49:38 +0100 Subject: [PATCH 09/13] bump --- CHANGELOG.md | 5 +++++ README.md | 1 + package.json | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 466e5c34..9e1c0b16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [v1.1.0] + +### Added +- `copy-github-permalink.view`: View and open Github's permalink in VS Code. + ## [v1.0.2] ### Fixed diff --git a/README.md b/README.md index cff61189..102ae15a 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ This extension by default will copy permalink to your `origin/master` because it To activate, enter these commands in the Command Palette: - `copy-github-permalink.copy`: Copy permalink of the selected line. +- `copy-github-permalink.view`: View and open Github's permalink in VS Code. ## Extension Settings diff --git a/package.json b/package.json index 8193c238..1166938a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "displayName": "Copy Github Permalink", "description": "Copy Github Permalink", "icon": "img/ico.png", - "version": "1.0.2", + "version": "1.1.0", "homepage": "https://github.com/tejanium/vscode-copy-github-permalink/blob/master/README.md", "bugs": { "url": "https://github.com/tejanium/vscode-copy-github-permalink/issues" From 1f78b842a5a93a38fadc261b4772f20c0fa45add Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 21 Sep 2020 09:54:23 +0100 Subject: [PATCH 10/13] rename to show --- CHANGELOG.md | 2 +- README.md | 2 +- package.json | 6 +++--- src/extension.ts | 4 ++-- src/test/suite/extension.test.ts | 18 +++++++++--------- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e1c0b16..6d796322 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## [v1.1.0] ### Added -- `copy-github-permalink.view`: View and open Github's permalink in VS Code. +- `copy-github-permalink.show`: Show Github's permalink in VS Code. ## [v1.0.2] diff --git a/README.md b/README.md index 102ae15a..a18ed39a 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ This extension by default will copy permalink to your `origin/master` because it To activate, enter these commands in the Command Palette: - `copy-github-permalink.copy`: Copy permalink of the selected line. -- `copy-github-permalink.view`: View and open Github's permalink in VS Code. +- `copy-github-permalink.show`: Show Github's permalink in VS Code. ## Extension Settings diff --git a/package.json b/package.json index 1166938a..b0cdb453 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ ], "activationEvents": [ "onCommand:copy-github-permalink.copy", - "onCommand:copy-github-permalink.view" + "onCommand:copy-github-permalink.show" ], "main": "./dist/extension", "contributes": { @@ -31,8 +31,8 @@ "command": "copy-github-permalink.copy", "title": "Github Permalink: Copy" }, { - "command": "copy-github-permalink.view", - "title": "Github Permalink: View" + "command": "copy-github-permalink.show", + "title": "Github Permalink: Show" } ], "configuration": { diff --git a/src/extension.ts b/src/extension.ts index 8c45fcb7..531dd45e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -23,7 +23,7 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(copy); - let view = vscode.commands.registerCommand('copy-github-permalink.view', async () => { + let show = vscode.commands.registerCommand('copy-github-permalink.show', async () => { const input = await vscode.window.showInputBox(); if (input) { @@ -38,7 +38,7 @@ export function activate(context: vscode.ExtensionContext) { } }); - context.subscriptions.push(view); + context.subscriptions.push(show); } function getBranch(): string { diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index 841f058e..6e7962b2 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -125,47 +125,47 @@ suite('Test commands', () => { }); }); - suite('copy-github-permalink.view', () => { - test('Open the file', async () => { + suite('copy-github-permalink.show', () => { + test('Show the file', async () => { const showDocumentStub = sandbox.stub(vscode.window, 'showTextDocument') as unknown as sinon.SinonStub<[vscode.TextDocument, vscode.TextDocumentShowOptions]>; sandbox.stub(vscode.window, 'showInputBox').resolves('https://github.com/owner/name/blob/sha1234567890/test/fixtures/file.txt#L1-L3'); - await vscode.commands.executeCommand('copy-github-permalink.view'); + await vscode.commands.executeCommand('copy-github-permalink.show'); const document = await getFixtureFile('test/fixtures/file.txt'); const range = new vscode.Range(new vscode.Position(0, 0), new vscode.Position(3, 0)); sandbox.assert.calledWith(showDocumentStub, document, { preview: false, selection: range }); }); - test('Open the file even without sha', async () => { + test('Show the file even without sha', async () => { const showDocumentStub = sandbox.stub(vscode.window, 'showTextDocument') as unknown as sinon.SinonStub<[vscode.TextDocument, vscode.TextDocumentShowOptions]>; sandbox.stub(vscode.window, 'showInputBox').resolves('https://github.com/owner/name/blob/master/test/fixtures/file.txt'); - await vscode.commands.executeCommand('copy-github-permalink.view'); + await vscode.commands.executeCommand('copy-github-permalink.show'); const document = await getFixtureFile('test/fixtures/file.txt'); sandbox.assert.calledWith(showDocumentStub, document, { preview: false }); }); - test('Open the file even without line', async () => { + test('Show the file even without line', async () => { const showDocumentStub = sandbox.stub(vscode.window, 'showTextDocument') as unknown as sinon.SinonStub<[vscode.TextDocument, vscode.TextDocumentShowOptions]>; sandbox.stub(vscode.window, 'showInputBox').resolves('https://github.com/owner/name/blob/sha1234567890/test/fixtures/file.txt'); - await vscode.commands.executeCommand('copy-github-permalink.view'); + await vscode.commands.executeCommand('copy-github-permalink.show'); const document = await getFixtureFile('test/fixtures/file.txt'); sandbox.assert.calledWith(showDocumentStub, document, { preview: false }); }); - test('Open the file even with missing line', async () => { + test('Show the file even with missing line', async () => { const showDocumentStub = sandbox.stub(vscode.window, 'showTextDocument') as unknown as sinon.SinonStub<[vscode.TextDocument, vscode.TextDocumentShowOptions]>; sandbox.stub(vscode.window, 'showInputBox').resolves('https://github.com/owner/name/blob/sha1234567890/test/fixtures/file.txt#'); - await vscode.commands.executeCommand('copy-github-permalink.view'); + await vscode.commands.executeCommand('copy-github-permalink.show'); const document = await getFixtureFile('test/fixtures/file.txt'); sandbox.assert.calledWith(showDocumentStub, document, { preview: false }); From d8eb67daabc1432859d3e2e15f45f647be2a89ab Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 21 Sep 2020 10:02:32 +0100 Subject: [PATCH 11/13] const --- src/extension.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 531dd45e..3a8ebb68 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -3,7 +3,7 @@ import { Document } from './model/document'; import { Permalink } from './model/permalink'; export function activate(context: vscode.ExtensionContext) { - let copy = vscode.commands.registerCommand('copy-github-permalink.copy', async () => { + const copy = vscode.commands.registerCommand('copy-github-permalink.copy', async () => { const editor = vscode.window.activeTextEditor; if (editor && editor.document.uri.scheme === 'file') { @@ -23,7 +23,7 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(copy); - let show = vscode.commands.registerCommand('copy-github-permalink.show', async () => { + const show = vscode.commands.registerCommand('copy-github-permalink.show', async () => { const input = await vscode.window.showInputBox(); if (input) { From e3724d1dc5fd76fbc9ef9e69d890a7ea175938d9 Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 21 Sep 2020 10:05:29 +0100 Subject: [PATCH 12/13] asdf --- src/extension.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extension.ts b/src/extension.ts index 3a8ebb68..3cbfe582 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -27,8 +27,8 @@ export function activate(context: vscode.ExtensionContext) { const input = await vscode.window.showInputBox(); if (input) { - const showOpts = { preview: false }; const document = new Document(input); + const showOpts = { preview: false }; if (document.range) { Object.assign(showOpts, { selection: document.range }); From 875a74680f123d0a0044cef0ba18d1537294ab15 Mon Sep 17 00:00:00 2001 From: Teja Sophista Date: Mon, 21 Sep 2020 10:13:42 +0100 Subject: [PATCH 13/13] tabs --- src/model/document.ts | 46 +++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/model/document.ts b/src/model/document.ts index 17f4f7c9..3bc244ad 100644 --- a/src/model/document.ts +++ b/src/model/document.ts @@ -1,38 +1,38 @@ import * as vscode from 'vscode'; export class Document { - private path: string | undefined; - private position: string | undefined; + private path: string | undefined; + private position: string | undefined; constructor(private permalink: string) { - [this.path, this.position] = this.relativePathWithLines.split('#') || []; - } + [this.path, this.position] = this.relativePathWithLines.split('#') || []; + } - async document(): Promise { - const root = vscode.workspace.workspaceFolders![0].uri.fsPath; + async document(): Promise { + const root = vscode.workspace.workspaceFolders![0].uri.fsPath; - return await vscode.workspace.openTextDocument(`${root}/${this.path}`); - } + return await vscode.workspace.openTextDocument(`${root}/${this.path}`); + } get range(): vscode.Range | undefined { - if (!this.position) { - return; - } + if (!this.position) { + return; + } - const [start, end] = this.position.split("-") || []; - const startPosition = this.getPosition(start, -1); - const endPosition = this.getPosition(end); + const [start, end] = this.position.split("-") || []; + const startPosition = this.getPosition(start, -1); + const endPosition = this.getPosition(end); - return new vscode.Range(startPosition, endPosition); - } + return new vscode.Range(startPosition, endPosition); + } - private get relativePathWithLines(): string { - return this.permalink.split('blob').pop()?.split('/').slice(2).join('/') || ''; - } + private get relativePathWithLines(): string { + return this.permalink.split('blob').pop()?.split('/').slice(2).join('/') || ''; + } - private getPosition(raw: string | undefined, offset: number = 0) { - const safeRaw = raw?.replace(/\D/g, '') || '0'; + private getPosition(raw: string | undefined, offset: number = 0) { + const safeRaw = raw?.replace(/\D/g, '') || '0'; - return new vscode.Position(parseInt(safeRaw) + offset, 0); - } + return new vscode.Position(parseInt(safeRaw) + offset, 0); + } }