Skip to content

Commit 051ac80

Browse files
committed
feat: refactor and handle providers in the right way
Refactor to a more functional paradigm for all of the functionality. Avoiding `this` should make it so that we can avoid all of the possible errors that come with this. Also, the code should be more readable now. The big piece of this feature is making choosing the right provider work. Previously, if you had more than one "provider" then it would not properly go to definition.
1 parent e6e477b commit 051ac80

File tree

5 files changed

+121
-71
lines changed

5 files changed

+121
-71
lines changed

lib/atom-ide-definitions.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

lib/goToDefinition.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use babel';
2+
3+
async function goToDefinition(providerRegistry) {
4+
if (!providerRegistry) {
5+
return;
6+
}
7+
8+
const editor = atom.workspace.getActiveTextEditor();
9+
const provider = providerRegistry.getProviderForEditor(editor);
10+
11+
const position = editor.getCursorBufferPosition();
12+
const result = await provider.getDefinition(editor, position);
13+
14+
if (!result) {
15+
atom.notifications.addError('Sorry, no definitions found.');
16+
return;
17+
}
18+
19+
const { definitions } = result;
20+
21+
if (!definitions || definitions.length === 0) {
22+
atom.notifications.addError('Sorry, no definitions found.');
23+
return;
24+
}
25+
26+
const {
27+
path,
28+
position: { row, column },
29+
} = definitions[0];
30+
31+
if (editor.getPath() === path) {
32+
const paneContainer = atom.workspace.paneContainerForItem(editor);
33+
paneContainer.activate();
34+
35+
editor.setCursorBufferPosition([row, column]);
36+
editor.scrollToBufferPosition([row, column], { center: true });
37+
} else {
38+
await atom.workspace.open(path, {
39+
initialLine: row,
40+
initialColumn: column,
41+
searchAllPanes: true,
42+
activatePane: true,
43+
activateItem: true,
44+
});
45+
}
46+
}
47+
48+
export default goToDefinition;

lib/main.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use babel';
2+
3+
import { CompositeDisposable } from 'atom';
4+
import goToDefinition from './goToDefinition';
5+
import createProviderRegistry from './providerRegistry';
6+
7+
function package() {
8+
const providerRegistry = createProviderRegistry();
9+
let subscriptions;
10+
11+
function activate() {
12+
subscriptions = new CompositeDisposable();
13+
14+
subscriptions.add(
15+
atom.commands.add('atom-workspace', {
16+
'atom-ide-go-to-definition:go-to-definition': () =>
17+
goToDefinition(providerRegistry),
18+
})
19+
);
20+
}
21+
22+
function deactivate() {
23+
subscriptions.dispose();
24+
}
25+
26+
function consumeDefinitionsService(provider) {
27+
providerRegistry.addProvider(provider);
28+
}
29+
30+
return { activate, deactivate, consumeDefinitionsService };
31+
}
32+
33+
export default package();

lib/providerRegistry.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use babel';
2+
3+
import { Disposable } from 'atom';
4+
5+
function providerRegistry() {
6+
let providers = [];
7+
8+
function addProvider(provider) {
9+
const index = providers.findIndex(p => provider.priority > p.priority);
10+
if (index === -1) {
11+
providers.push(provider);
12+
} else {
13+
providers.splice(index, 0, provider);
14+
}
15+
16+
return new Disposable(() => removeProvider(provider));
17+
}
18+
19+
function removeProvider(provider) {
20+
const index = providers.indexOf(provider);
21+
if (index !== -1) {
22+
providers.splice(index, 1);
23+
}
24+
}
25+
26+
function getProviderForEditor(editor) {
27+
const grammar = editor.getGrammar().scopeName;
28+
29+
return providers.find(
30+
provider =>
31+
!provider.grammarScopes ||
32+
provider.grammarScopes.indexOf(grammar) !== -1
33+
);
34+
}
35+
36+
return { addProvider, getProviderForEditor };
37+
}
38+
39+
export default providerRegistry;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "atom-ide-definitions",
3-
"main": "./lib/atom-ide-definitions",
3+
"main": "./lib/main",
44
"version": "0.1.3",
55
"description": "A replacement for the Definitions functionality that was originally part of the Atom IDE package from Facebook.",
66
"keywords": [

0 commit comments

Comments
 (0)