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

Commit 5680f58

Browse files
committed
Implement using active panel to determine active project
1 parent 6d10136 commit 5680f58

File tree

3 files changed

+131
-8
lines changed

3 files changed

+131
-8
lines changed

lib/github-package.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default class GithubPackage {
6060
this.confirm = confirm;
6161
this.startOpen = false;
6262
this.activated = false;
63+
this.projectFromActivePanel = false;
6364

6465
const criteria = {
6566
projectPathCount: this.project.getPaths().length,
@@ -181,8 +182,26 @@ export default class GithubPackage {
181182
this.scheduleActiveContextUpdate({activeRepositoryPath});
182183
};
183184

185+
const handleActivePaneChange = item => {
186+
if (!this.projectFromActivePanel || !item || !this.workspace.isTextEditor(item) || !item.getBuffer() || !item.getBuffer().getPath()) {
187+
return;
188+
}
189+
190+
const activeDirectory = this.project.getDirectories().find(directory => {
191+
return directory.contains(item.getBuffer().getPath());
192+
});
193+
const activeRepositoryPath = activeDirectory ? activeDirectory.getPath() : undefined;
194+
this.scheduleActiveContextUpdate({activeRepositoryPath}, {item});
195+
};
196+
197+
const handleUseProjectFromActivePanelChange = newValue => {
198+
this.projectFromActivePanel = newValue;
199+
};
200+
184201
this.subscriptions.add(
185202
this.project.onDidChangePaths(handleProjectPathsChange),
203+
this.workspace.getCenter().onDidStopChangingActivePaneItem(handleActivePaneChange),
204+
this.config.observe('github.useProjectFromActivePanel', handleUseProjectFromActivePanelChange),
186205
this.styleCalculator.startWatching(
187206
'github-package-styles',
188207
['editor.fontSize', 'editor.fontFamily', 'editor.lineHeight', 'editor.tabLength'],

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@
209209
"ssh"
210210
],
211211
"description": "Transport protocol to prefer when creating a new git remote"
212+
},
213+
"useProjectFromActivePanel": {
214+
"type": "boolean",
215+
"default": false,
216+
"description": "Use active panel to determine which project to show"
212217
}
213218
},
214219
"deserializers": {

test/github-package.test.js

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import GithubPackage from '../lib/github-package';
1010
describe('GithubPackage', function() {
1111

1212
async function buildAtomEnvironmentAndGithubPackage(buildAtomEnvironment, options = {}) {
13-
const atomEnv = global.buildAtomEnvironment();
13+
const atomEnv = buildAtomEnvironment();
1414
await disableFilesystemWatchers(atomEnv);
1515

1616
const packageOptions = {
@@ -147,7 +147,7 @@ describe('GithubPackage', function() {
147147
({
148148
atomEnv, githubPackage,
149149
project, config, configDirPath, contextPool,
150-
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironmentAndGithubPackage));
150+
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironment));
151151
});
152152

153153
afterEach(async function() {
@@ -237,6 +237,105 @@ describe('GithubPackage', function() {
237237
});
238238
});
239239

240+
describe('with 3 projects and unactivated', function() {
241+
let workdirPath1, workdirPath2, workdirPath3;
242+
beforeEach(async function() {
243+
([workdirPath1, workdirPath2, workdirPath3] = await Promise.all([
244+
cloneRepository('three-files'),
245+
cloneRepository('three-files'),
246+
cloneRepository('three-files'),
247+
]));
248+
project.setPaths([workdirPath1, workdirPath2, workdirPath3]);
249+
});
250+
251+
describe('with useProjectFromActivePanel config enabled', function() {
252+
beforeEach(async function() {
253+
atomEnv.config.set('github.useProjectFromActivePanel', true);
254+
await contextUpdateAfter(githubPackage, () => githubPackage.activate({}));
255+
});
256+
257+
it('changes repository when active panel is changed', async function() {
258+
await Promise.all([
259+
atomEnv.workspace.open(path.join(workdirPath1, 'a.txt')),
260+
new Promise((resolve, reject) => {
261+
githubPackage.scheduleActiveContextUpdate = ({activeRepositoryPath}) => {
262+
try {
263+
assert.strictEqual(activeRepositoryPath, workdirPath1);
264+
resolve();
265+
} catch (e) { reject(e); }
266+
};
267+
}),
268+
]);
269+
270+
await Promise.all([
271+
atomEnv.workspace.open(path.join(workdirPath2, 'b.txt')),
272+
new Promise((resolve, reject) => {
273+
githubPackage.scheduleActiveContextUpdate = ({activeRepositoryPath}) => {
274+
try {
275+
assert.strictEqual(activeRepositoryPath, workdirPath2);
276+
resolve();
277+
} catch (e) { reject(e); }
278+
};
279+
}),
280+
]);
281+
282+
await Promise.all([
283+
atomEnv.workspace.open(path.join(workdirPath3, 'c.txt')),
284+
new Promise((resolve, reject) => {
285+
githubPackage.scheduleActiveContextUpdate = ({activeRepositoryPath}) => {
286+
try {
287+
assert.strictEqual(activeRepositoryPath, workdirPath3);
288+
resolve();
289+
} catch (e) { reject(e); }
290+
};
291+
}),
292+
]);
293+
});
294+
295+
it('subscribes to changes in the configuration value', async function() {
296+
const scheduleActiveContextUpdate = githubPackage.scheduleActiveContextUpdate = sinon.stub();
297+
298+
await Promise.all([
299+
atomEnv.workspace.open(path.join(workdirPath1, 'a.txt')),
300+
new Promise((resolve, reject) => {
301+
scheduleActiveContextUpdate.callsFake(({activeRepositoryPath}) => {
302+
try {
303+
assert.strictEqual(activeRepositoryPath, workdirPath1);
304+
resolve();
305+
} catch (e) { reject(e); }
306+
});
307+
}),
308+
]);
309+
310+
atomEnv.config.set('github.useProjectFromActivePanel', false);
311+
await Promise.all([
312+
atomEnv.workspace.open(path.join(workdirPath2, 'b.txt')),
313+
new Promise(resolve => {
314+
const disposable = atomEnv.workspace.getCenter().onDidStopChangingActivePaneItem(() => {
315+
disposable.dispose();
316+
resolve();
317+
});
318+
}),
319+
]);
320+
321+
atomEnv.config.set('github.useProjectFromActivePanel', true);
322+
await Promise.all([
323+
atomEnv.workspace.open(path.join(workdirPath3, 'c.txt')),
324+
new Promise((resolve, reject) => {
325+
scheduleActiveContextUpdate.callsFake(({activeRepositoryPath}) => {
326+
try {
327+
assert.strictEqual(activeRepositoryPath, workdirPath3);
328+
resolve();
329+
} catch (e) { reject(e); }
330+
});
331+
}),
332+
]);
333+
334+
assert.strictEqual(scheduleActiveContextUpdate.callCount, 2);
335+
});
336+
});
337+
});
338+
240339
describe('with 1 project and absent state', function() {
241340
let workdirPath1, workdirPath2, context1;
242341
beforeEach(async function() {
@@ -357,7 +456,7 @@ describe('GithubPackage', function() {
357456
({
358457
atomEnv, githubPackage,
359458
project, contextPool,
360-
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironmentAndGithubPackage));
459+
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironment));
361460
});
362461

363462
afterEach(async function() {
@@ -664,7 +763,7 @@ describe('GithubPackage', function() {
664763
({
665764
atomEnv, githubPackage,
666765
project, contextPool,
667-
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironmentAndGithubPackage));
766+
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironment));
668767
});
669768

670769
afterEach(async function() {
@@ -706,7 +805,7 @@ describe('GithubPackage', function() {
706805
({
707806
atomEnv, githubPackage,
708807
project,
709-
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironmentAndGithubPackage));
808+
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironment));
710809
});
711810

712811
afterEach(async function() {
@@ -774,7 +873,7 @@ describe('GithubPackage', function() {
774873
beforeEach(async function() {
775874
({
776875
atomEnv, githubPackage,
777-
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironmentAndGithubPackage));
876+
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironment));
778877

779878
sinon.spy(githubPackage, 'rerender');
780879
});
@@ -825,7 +924,7 @@ describe('GithubPackage', function() {
825924
beforeEach(async function() {
826925
({
827926
atomEnv, githubPackage,
828-
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironmentAndGithubPackage));
927+
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironment));
829928

830929
sinon.spy(githubPackage, 'rerender');
831930
});
@@ -879,7 +978,7 @@ describe('GithubPackage', function() {
879978
({
880979
atomEnv, githubPackage,
881980
project, contextPool,
882-
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironmentAndGithubPackage));
981+
} = await buildAtomEnvironmentAndGithubPackage(global.buildAtomEnvironment));
883982
});
884983

885984
let workdirPath1, atomGitRepository1, repository1;

0 commit comments

Comments
 (0)