From 45c098c55f3bcc6627e7c7b734f1a00af3e210a2 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Wed, 14 Aug 2019 15:33:06 -0500 Subject: [PATCH 1/5] get target from command event --- lib/main.js | 138 +++++++++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 67 deletions(-) diff --git a/lib/main.js b/lib/main.js index 4090fc8..228dd58 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,11 +1,51 @@ const {Disposable} = require('atom') const GitHubFile = require('./github-file') -function getActivePath () { - const activePaneItem = atom.workspace.getActivePaneItem() +function getActivePath (target) { + if (!target) { + return atom.project.getPaths()[0]; + } + + const treeView = target.closest(".tree-view"); + if (treeView) { + // called from treeview + const selected = treeView.querySelector(".selected > .list-item > .name, .selected > .name"); + if (selected) { + return selected.dataset.path; + } + return; + } + + const tab = target.closest(".tab-bar > .tab"); + if (tab) { + // called from tab + const title = tab.querySelector(".title"); + if (title && title.dataset.path) { + return title.dataset.path; + } + return; + } - if (activePaneItem && typeof activePaneItem.getPath === 'function') { - return activePaneItem.getPath() + const paneItem = atom.workspace.getActivePaneItem(); + if (paneItem && typeof paneItem.getPath === "function") { + // called from active pane + return paneItem.getPath(); + } + + const textEditor = atom.workspace.getActiveTextEditor(); + if (textEditor && typeof textEditor.getPath === "function") { + // fallback to activeTextEditor if activePaneItem is not a file + return textEditor.getPath(); + } +} + +function pathCommand(func) { + return function (e) { + const itemPath = getActivePath(e.target) + + if (itemPath) { + func(itemPath); + } } } @@ -21,77 +61,41 @@ module.exports = { activate () { this.commandsSubscription = new Disposable() this.commandsSubscription = atom.commands.add('atom-pane', { - 'open-on-github:file': () => { - const itemPath = getActivePath() - - if (itemPath) { - GitHubFile.fromPath(itemPath).open(getSelectedRange()) - } - }, - - 'open-on-github:file-on-master': () => { - const itemPath = getActivePath() - - if (itemPath) { - GitHubFile.fromPath(itemPath).openOnMaster(getSelectedRange()) - } - }, - - 'open-on-github:blame': () => { - const itemPath = getActivePath() - - if (itemPath) { - GitHubFile.fromPath(itemPath).blame(getSelectedRange()) - } - }, - - 'open-on-github:history': () => { - const itemPath = getActivePath() - - if (itemPath) { - GitHubFile.fromPath(itemPath).history() - } - }, - - 'open-on-github:issues': () => { - const itemPath = getActivePath() - - if (itemPath) { - GitHubFile.fromPath(itemPath).openIssues() - } - }, + 'open-on-github:file': pathCommand((itemPath) => { + GitHubFile.fromPath(itemPath).open(getSelectedRange()) + }), - 'open-on-github:pull-requests': () => { - const itemPath = getActivePath() + 'open-on-github:file-on-master': pathCommand((itemPath) => { + GitHubFile.fromPath(itemPath).openOnMaster(getSelectedRange()) + }), - if (itemPath) { - return GitHubFile.fromPath(itemPath).openPullRequests() - } - }, + 'open-on-github:blame': pathCommand((itemPath) => { + GitHubFile.fromPath(itemPath).blame(getSelectedRange()) + }), - 'open-on-github:copy-url': () => { - const itemPath = getActivePath() + 'open-on-github:history': pathCommand((itemPath) => { + GitHubFile.fromPath(itemPath).history() + }), - if (itemPath) { - GitHubFile.fromPath(itemPath).copyURL(getSelectedRange()) - } - }, + 'open-on-github:issues': pathCommand((itemPath) => { + GitHubFile.fromPath(itemPath).openIssues() + }), - 'open-on-github:branch-compare': () => { - const itemPath = getActivePath() + 'open-on-github:pull-requests': pathCommand((itemPath) => { + GitHubFile.fromPath(itemPath).openPullRequests() + }), - if (itemPath) { - GitHubFile.fromPath(itemPath).openBranchCompare() - } - }, + 'open-on-github:copy-url': pathCommand((itemPath) => { + GitHubFile.fromPath(itemPath).copyURL(getSelectedRange()) + }), - 'open-on-github:repository': () => { - const itemPath = getActivePath() + 'open-on-github:branch-compare': pathCommand((itemPath) => { + GitHubFile.fromPath(itemPath).openBranchCompare() + }), - if (itemPath) { - GitHubFile.fromPath(itemPath).openRepository() - } - } + 'open-on-github:repository': pathCommand((itemPath) => { + GitHubFile.fromPath(itemPath).openRepository() + }) }) }, From 076a9bcc89da8b0b4b456aa55ffd124a28a9cedb Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Wed, 14 Aug 2019 15:45:43 -0500 Subject: [PATCH 2/5] lint --- lib/main.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/main.js b/lib/main.js index 228dd58..4863938 100644 --- a/lib/main.js +++ b/lib/main.js @@ -3,48 +3,48 @@ const GitHubFile = require('./github-file') function getActivePath (target) { if (!target) { - return atom.project.getPaths()[0]; + return atom.project.getPaths()[0] } - const treeView = target.closest(".tree-view"); + const treeView = target.closest('.tree-view') if (treeView) { // called from treeview - const selected = treeView.querySelector(".selected > .list-item > .name, .selected > .name"); + const selected = treeView.querySelector('.selected > .list-item > .name, .selected > .name') if (selected) { - return selected.dataset.path; + return selected.dataset.path } - return; + return } - const tab = target.closest(".tab-bar > .tab"); + const tab = target.closest('.tab-bar > .tab') if (tab) { // called from tab - const title = tab.querySelector(".title"); + const title = tab.querySelector('.title') if (title && title.dataset.path) { - return title.dataset.path; + return title.dataset.path } - return; + return } - const paneItem = atom.workspace.getActivePaneItem(); - if (paneItem && typeof paneItem.getPath === "function") { + const paneItem = atom.workspace.getActivePaneItem() + if (paneItem && typeof paneItem.getPath === 'function') { // called from active pane - return paneItem.getPath(); + return paneItem.getPath() } - const textEditor = atom.workspace.getActiveTextEditor(); - if (textEditor && typeof textEditor.getPath === "function") { + const textEditor = atom.workspace.getActiveTextEditor() + if (textEditor && typeof textEditor.getPath === 'function') { // fallback to activeTextEditor if activePaneItem is not a file - return textEditor.getPath(); + return textEditor.getPath() } } -function pathCommand(func) { +function pathCommand (func) { return function (e) { const itemPath = getActivePath(e.target) if (itemPath) { - func(itemPath); + func(itemPath) } } } From d9e24a916e18b4ccaddce481a8b16c72a4a831b3 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Tue, 20 Aug 2019 12:29:02 -0500 Subject: [PATCH 3/5] add project fixture --- spec/fixtures/project/file1.txt | 0 spec/fixtures/project/file2.txt | 0 spec/fixtures/project/img1.jpg | Bin 0 -> 732 bytes 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 spec/fixtures/project/file1.txt create mode 100644 spec/fixtures/project/file2.txt create mode 100644 spec/fixtures/project/img1.jpg diff --git a/spec/fixtures/project/file1.txt b/spec/fixtures/project/file1.txt new file mode 100644 index 0000000..e69de29 diff --git a/spec/fixtures/project/file2.txt b/spec/fixtures/project/file2.txt new file mode 100644 index 0000000..e69de29 diff --git a/spec/fixtures/project/img1.jpg b/spec/fixtures/project/img1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..14465a58c1ccdd30aafae6d13d01df3ba8b55982 GIT binary patch literal 732 zcmex=X!XqN1l2cOC(lau%ic3n%$}1|X znp;}i+B-VCCQY6)b=ve9GiNPYykzOJeA&a zSFc^aar4&0M~|O8efIpt%U2&ieg5+G+xH(oe}VkP$iNKo7Ldg1FF~Ncm_TvB4)PZx zQ#lYb3$m~(8nOvF2C^p>3M&~ka)>xhT)6Qdr?PR-2hpUWi(FzVCJ$9Vg1iRy8F3zK gBFkrRk0JbZi-Cuk5g2*Qf(-TyfB!6Mu>XG(09204&j0`b literal 0 HcmV?d00001 From f8bd3febb7f816789e0e09d19fc9fd198bfc2cb0 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Tue, 20 Aug 2019 12:29:49 -0500 Subject: [PATCH 4/5] move getActivePath to separate file --- lib/get-active-path.js | 45 ++++++++++++++++++++++++++++++++++++++++++ lib/main.js | 39 +----------------------------------- 2 files changed, 46 insertions(+), 38 deletions(-) create mode 100644 lib/get-active-path.js diff --git a/lib/get-active-path.js b/lib/get-active-path.js new file mode 100644 index 0000000..e75ed7e --- /dev/null +++ b/lib/get-active-path.js @@ -0,0 +1,45 @@ +function getActivePath (target) { + if (!target) { + return atom.project.getPaths()[0] + } + + const treeView = target.closest('.tree-view') + if (treeView) { + // called from treeview + const selected = treeView.querySelector('.selected > .list-item > .name, .selected > .name') + if (selected) { + return selected.dataset.path + } + return + } + + const tab = target.closest('.tab-bar > .tab') + if (tab) { + // called from tab + const title = tab.querySelector('.title') + if (title && title.dataset.path) { + return title.dataset.path + } + return + } + + const paneItem = atom.workspace.getActivePaneItem() + if (paneItem && typeof paneItem.getPath === 'function') { + // called from active pane + return paneItem.getPath() + } + + const textEditor = atom.workspace.getActiveTextEditor() + if (textEditor && typeof textEditor.getPath === 'function') { + // fallback to activeTextEditor if activePaneItem is not a file + return textEditor.getPath() + } + + const projects = atom.project.getPaths() + if (projects.length === 1) { + // use project is nothing is open + return projects[0] + } +} + +module.exports = getActivePath diff --git a/lib/main.js b/lib/main.js index 4863938..84ac790 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,43 +1,6 @@ const {Disposable} = require('atom') const GitHubFile = require('./github-file') - -function getActivePath (target) { - if (!target) { - return atom.project.getPaths()[0] - } - - const treeView = target.closest('.tree-view') - if (treeView) { - // called from treeview - const selected = treeView.querySelector('.selected > .list-item > .name, .selected > .name') - if (selected) { - return selected.dataset.path - } - return - } - - const tab = target.closest('.tab-bar > .tab') - if (tab) { - // called from tab - const title = tab.querySelector('.title') - if (title && title.dataset.path) { - return title.dataset.path - } - return - } - - const paneItem = atom.workspace.getActivePaneItem() - if (paneItem && typeof paneItem.getPath === 'function') { - // called from active pane - return paneItem.getPath() - } - - const textEditor = atom.workspace.getActiveTextEditor() - if (textEditor && typeof textEditor.getPath === 'function') { - // fallback to activeTextEditor if activePaneItem is not a file - return textEditor.getPath() - } -} +const getActivePath = require('./get-active-path') function pathCommand (func) { return function (e) { From b5aab0e4a17d2cce76d7942f8e02b066f6d6883e Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Tue, 20 Aug 2019 12:30:32 -0500 Subject: [PATCH 5/5] add tests --- spec/get-active-path-spec.js | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 spec/get-active-path-spec.js diff --git a/spec/get-active-path-spec.js b/spec/get-active-path-spec.js new file mode 100644 index 0000000..261909f --- /dev/null +++ b/spec/get-active-path-spec.js @@ -0,0 +1,75 @@ +const path = require('path') +const getActivePath = require('../lib/get-active-path') + +const { it, fit, beforeEach, afterEach } = require('./async-spec-helpers') // eslint-disable-line no-unused-vars + +const projectPath = path.resolve(__dirname, './fixtures/project/') +const file1 = path.resolve(__dirname, './fixtures/project/file1.txt') +const file2 = path.resolve(__dirname, './fixtures/project/file2.txt') +const img1 = path.resolve(__dirname, './fixtures/project/img1.png') + +describe('getActivePath', function () { + let workspaceElement + beforeEach(async function () { + workspaceElement = atom.views.getView(atom.workspace) + await atom.packages.activatePackage('tabs') + await atom.packages.activatePackage('tree-view') + atom.project.setPaths([projectPath]) + }) + + it('returns project path when no target', function () { + const itemPath = getActivePath() + expect(itemPath).toBe(projectPath) + }) + + it('returns project path when nothing open', function () { + const itemPath = getActivePath(workspaceElement) + expect(itemPath).toBe(projectPath) + }) + + it('returns active file path when workspace is selected', async function () { + await atom.workspace.open(file1) + await atom.workspace.open(file2) + + const itemPath = getActivePath(workspaceElement) + expect(itemPath).toBe(file2) + }) + + it('returns file path when tree view is selected', async function () { + await atom.workspace.open(file1) + await atom.workspace.open(file2) + + const { treeView } = atom.packages.getLoadedPackage('tree-view').mainModule + const file1Target = treeView.selectEntryForPath(file1) + + const itemPath = getActivePath(file1Target) + expect(itemPath).toBe(file1) + }) + + it('returns file path when tab is selected', async function () { + await atom.workspace.open(file1) + await atom.workspace.open(file2) + const file1Target = workspaceElement.querySelector(".tab-bar [data-name='file1.txt']") + + const itemPath = getActivePath(file1Target) + expect(itemPath).toBe(file1) + }) + + it('returns project when active pane is not a file', async function () { + await atom.packages.activatePackage('settings-view') + await atom.workspace.open(file1) + await atom.workspace.open('atom://config') + + const itemPath = getActivePath(workspaceElement) + expect(itemPath).toBe(projectPath) + }) + + it('returns active pane path when it is not a text file', async function () { + await atom.packages.activatePackage('image-view') + await atom.workspace.open(file1) + await atom.workspace.open(img1) + + const itemPath = getActivePath(workspaceElement) + expect(itemPath).toBe(img1) + }) +})