Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit b0bb91f

Browse files
authored
Merge pull request #111 from UziTech/project-path
Get target from command target
2 parents 139dd0e + b5aab0e commit b0bb91f

File tree

6 files changed

+154
-67
lines changed

6 files changed

+154
-67
lines changed

lib/get-active-path.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function getActivePath (target) {
2+
if (!target) {
3+
return atom.project.getPaths()[0]
4+
}
5+
6+
const treeView = target.closest('.tree-view')
7+
if (treeView) {
8+
// called from treeview
9+
const selected = treeView.querySelector('.selected > .list-item > .name, .selected > .name')
10+
if (selected) {
11+
return selected.dataset.path
12+
}
13+
return
14+
}
15+
16+
const tab = target.closest('.tab-bar > .tab')
17+
if (tab) {
18+
// called from tab
19+
const title = tab.querySelector('.title')
20+
if (title && title.dataset.path) {
21+
return title.dataset.path
22+
}
23+
return
24+
}
25+
26+
const paneItem = atom.workspace.getActivePaneItem()
27+
if (paneItem && typeof paneItem.getPath === 'function') {
28+
// called from active pane
29+
return paneItem.getPath()
30+
}
31+
32+
const textEditor = atom.workspace.getActiveTextEditor()
33+
if (textEditor && typeof textEditor.getPath === 'function') {
34+
// fallback to activeTextEditor if activePaneItem is not a file
35+
return textEditor.getPath()
36+
}
37+
38+
const projects = atom.project.getPaths()
39+
if (projects.length === 1) {
40+
// use project is nothing is open
41+
return projects[0]
42+
}
43+
}
44+
45+
module.exports = getActivePath

lib/main.js

Lines changed: 34 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
const {Disposable} = require('atom')
22
const GitHubFile = require('./github-file')
3+
const getActivePath = require('./get-active-path')
34

4-
function getActivePath () {
5-
const activePaneItem = atom.workspace.getActivePaneItem()
5+
function pathCommand (func) {
6+
return function (e) {
7+
const itemPath = getActivePath(e.target)
68

7-
if (activePaneItem && typeof activePaneItem.getPath === 'function') {
8-
return activePaneItem.getPath()
9+
if (itemPath) {
10+
func(itemPath)
11+
}
912
}
1013
}
1114

@@ -21,77 +24,41 @@ module.exports = {
2124
activate () {
2225
this.commandsSubscription = new Disposable()
2326
this.commandsSubscription = atom.commands.add('atom-pane', {
24-
'open-on-github:file': () => {
25-
const itemPath = getActivePath()
26-
27-
if (itemPath) {
28-
GitHubFile.fromPath(itemPath).open(getSelectedRange())
29-
}
30-
},
31-
32-
'open-on-github:file-on-master': () => {
33-
const itemPath = getActivePath()
34-
35-
if (itemPath) {
36-
GitHubFile.fromPath(itemPath).openOnMaster(getSelectedRange())
37-
}
38-
},
39-
40-
'open-on-github:blame': () => {
41-
const itemPath = getActivePath()
42-
43-
if (itemPath) {
44-
GitHubFile.fromPath(itemPath).blame(getSelectedRange())
45-
}
46-
},
47-
48-
'open-on-github:history': () => {
49-
const itemPath = getActivePath()
50-
51-
if (itemPath) {
52-
GitHubFile.fromPath(itemPath).history()
53-
}
54-
},
55-
56-
'open-on-github:issues': () => {
57-
const itemPath = getActivePath()
58-
59-
if (itemPath) {
60-
GitHubFile.fromPath(itemPath).openIssues()
61-
}
62-
},
27+
'open-on-github:file': pathCommand((itemPath) => {
28+
GitHubFile.fromPath(itemPath).open(getSelectedRange())
29+
}),
6330

64-
'open-on-github:pull-requests': () => {
65-
const itemPath = getActivePath()
31+
'open-on-github:file-on-master': pathCommand((itemPath) => {
32+
GitHubFile.fromPath(itemPath).openOnMaster(getSelectedRange())
33+
}),
6634

67-
if (itemPath) {
68-
return GitHubFile.fromPath(itemPath).openPullRequests()
69-
}
70-
},
35+
'open-on-github:blame': pathCommand((itemPath) => {
36+
GitHubFile.fromPath(itemPath).blame(getSelectedRange())
37+
}),
7138

72-
'open-on-github:copy-url': () => {
73-
const itemPath = getActivePath()
39+
'open-on-github:history': pathCommand((itemPath) => {
40+
GitHubFile.fromPath(itemPath).history()
41+
}),
7442

75-
if (itemPath) {
76-
GitHubFile.fromPath(itemPath).copyURL(getSelectedRange())
77-
}
78-
},
43+
'open-on-github:issues': pathCommand((itemPath) => {
44+
GitHubFile.fromPath(itemPath).openIssues()
45+
}),
7946

80-
'open-on-github:branch-compare': () => {
81-
const itemPath = getActivePath()
47+
'open-on-github:pull-requests': pathCommand((itemPath) => {
48+
GitHubFile.fromPath(itemPath).openPullRequests()
49+
}),
8250

83-
if (itemPath) {
84-
GitHubFile.fromPath(itemPath).openBranchCompare()
85-
}
86-
},
51+
'open-on-github:copy-url': pathCommand((itemPath) => {
52+
GitHubFile.fromPath(itemPath).copyURL(getSelectedRange())
53+
}),
8754

88-
'open-on-github:repository': () => {
89-
const itemPath = getActivePath()
55+
'open-on-github:branch-compare': pathCommand((itemPath) => {
56+
GitHubFile.fromPath(itemPath).openBranchCompare()
57+
}),
9058

91-
if (itemPath) {
92-
GitHubFile.fromPath(itemPath).openRepository()
93-
}
94-
}
59+
'open-on-github:repository': pathCommand((itemPath) => {
60+
GitHubFile.fromPath(itemPath).openRepository()
61+
})
9562
})
9663
},
9764

spec/fixtures/project/file1.txt

Whitespace-only changes.

spec/fixtures/project/file2.txt

Whitespace-only changes.

spec/fixtures/project/img1.jpg

732 Bytes
Loading

spec/get-active-path-spec.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const path = require('path')
2+
const getActivePath = require('../lib/get-active-path')
3+
4+
const { it, fit, beforeEach, afterEach } = require('./async-spec-helpers') // eslint-disable-line no-unused-vars
5+
6+
const projectPath = path.resolve(__dirname, './fixtures/project/')
7+
const file1 = path.resolve(__dirname, './fixtures/project/file1.txt')
8+
const file2 = path.resolve(__dirname, './fixtures/project/file2.txt')
9+
const img1 = path.resolve(__dirname, './fixtures/project/img1.png')
10+
11+
describe('getActivePath', function () {
12+
let workspaceElement
13+
beforeEach(async function () {
14+
workspaceElement = atom.views.getView(atom.workspace)
15+
await atom.packages.activatePackage('tabs')
16+
await atom.packages.activatePackage('tree-view')
17+
atom.project.setPaths([projectPath])
18+
})
19+
20+
it('returns project path when no target', function () {
21+
const itemPath = getActivePath()
22+
expect(itemPath).toBe(projectPath)
23+
})
24+
25+
it('returns project path when nothing open', function () {
26+
const itemPath = getActivePath(workspaceElement)
27+
expect(itemPath).toBe(projectPath)
28+
})
29+
30+
it('returns active file path when workspace is selected', async function () {
31+
await atom.workspace.open(file1)
32+
await atom.workspace.open(file2)
33+
34+
const itemPath = getActivePath(workspaceElement)
35+
expect(itemPath).toBe(file2)
36+
})
37+
38+
it('returns file path when tree view is selected', async function () {
39+
await atom.workspace.open(file1)
40+
await atom.workspace.open(file2)
41+
42+
const { treeView } = atom.packages.getLoadedPackage('tree-view').mainModule
43+
const file1Target = treeView.selectEntryForPath(file1)
44+
45+
const itemPath = getActivePath(file1Target)
46+
expect(itemPath).toBe(file1)
47+
})
48+
49+
it('returns file path when tab is selected', async function () {
50+
await atom.workspace.open(file1)
51+
await atom.workspace.open(file2)
52+
const file1Target = workspaceElement.querySelector(".tab-bar [data-name='file1.txt']")
53+
54+
const itemPath = getActivePath(file1Target)
55+
expect(itemPath).toBe(file1)
56+
})
57+
58+
it('returns project when active pane is not a file', async function () {
59+
await atom.packages.activatePackage('settings-view')
60+
await atom.workspace.open(file1)
61+
await atom.workspace.open('atom://config')
62+
63+
const itemPath = getActivePath(workspaceElement)
64+
expect(itemPath).toBe(projectPath)
65+
})
66+
67+
it('returns active pane path when it is not a text file', async function () {
68+
await atom.packages.activatePackage('image-view')
69+
await atom.workspace.open(file1)
70+
await atom.workspace.open(img1)
71+
72+
const itemPath = getActivePath(workspaceElement)
73+
expect(itemPath).toBe(img1)
74+
})
75+
})

0 commit comments

Comments
 (0)