Skip to content

Commit

Permalink
Merge pull request #12 from atom-ide-community/fix-click-without-defi…
Browse files Browse the repository at this point in the history
…nition

fix: items without definition being active for click
  • Loading branch information
Belar authored Jan 28, 2020
2 parents fd9bc0a + 8099fb2 commit e0200fa
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 100 deletions.
27 changes: 19 additions & 8 deletions lib/clickProvider.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
'use babel';

import { goToDefinition, getDefinitions } from './goToDefinition';

export class ClickProvider {
constructor(options) {
this.clickHandler = options.clickHandler;
this.providerRegistry = options.providerRegistry;

this.suggestionForWordHandler = this.suggestionForWordHandler.bind(this);
this.getProvider = this.getProvider.bind(this);
}

suggestionForWordHandler(textEditor, text, range) {
const targetValue = text && text.trim();
if (!targetValue) return;
async suggestionForWordHandler(editor, text, range) {
const { start: position } = range;
const definitions = await getDefinitions(this.providerRegistry, {
editor,
position,
});
const hasDefinitions = definitions && !!definitions[0];

return {
range,
callback: () => this.clickHandler(),
};
const suggestion = hasDefinitions
? {
range,
callback: () =>
goToDefinition(this.providerRegistry, { editor, definitions }),
}
: false;

return suggestion;
}

getProvider() {
Expand Down
37 changes: 22 additions & 15 deletions lib/goToDefinition.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
'use babel';

async function goToDefinition(providerRegistry) {
if (!providerRegistry) {
export async function goToDefinition(
providerRegistry,
{ editor, definitions: knownDefinitions } = {}
) {
if (!editor) {
return;
}

const editor = atom.workspace.getActiveTextEditor();
const provider = providerRegistry.getProviderForEditor(editor);

const position = editor.getCursorBufferPosition();
const result = await provider.getDefinition(editor, position);

if (!result) {
atom.notifications.addError('Sorry, no definitions found.');
return;
}

const { definitions } = result;
const definitions =
knownDefinitions || (await getDefinitions(providerRegistry, { editor }));

if (!definitions || definitions.length === 0) {
atom.notifications.addError('Sorry, no definitions found.');
Expand Down Expand Up @@ -45,4 +38,18 @@ async function goToDefinition(providerRegistry) {
}
}

export default goToDefinition;
export async function getDefinitions(
providerRegistry,
{ editor, position: targetPosition } = {}
) {
if (!providerRegistry || !editor) {
return;
}

const position = targetPosition || editor.getCursorBufferPosition();

const provider = providerRegistry.getProviderForEditor(editor);
const result = await provider.getDefinition(editor, position);

return result && result.definitions;
}
10 changes: 6 additions & 4 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import { CompositeDisposable } from 'atom';
import { install as installPackageDependencies } from 'atom-package-deps';
import goToDefinition from './goToDefinition';
import { goToDefinition } from './goToDefinition';
import createProviderRegistry from './providerRegistry';
import { ClickProvider } from './clickProvider';

function package() {
const providerRegistry = createProviderRegistry();
const clickProvider = new ClickProvider({
clickHandler: () => goToDefinition(providerRegistry),
providerRegistry,
});
let subscriptions;

Expand All @@ -18,8 +18,10 @@ function package() {

subscriptions.add(
atom.commands.add('atom-workspace', {
'atom-ide-go-to-definition:go-to-definition': () =>
goToDefinition(providerRegistry),
'atom-ide-go-to-definition:go-to-definition': () => {
const editor = atom.workspace.getActiveTextEditor();
goToDefinition(providerRegistry, { editor });
},
})
);

Expand Down
73 changes: 0 additions & 73 deletions spec/atom-ide-click-spec.js

This file was deleted.

74 changes: 74 additions & 0 deletions spec/click-provider-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use babel';

import { ClickProvider } from '../lib/clickProvider';
import * as goToDefinition from '../lib/goToDefinition';

// Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs.
//
// To run a specific `it` or `describe` block add an `f` to the front (e.g. `fit`
// or `fdescribe`). Remove the `f` to unfocus the block.

describe('click provider', () => {
const editor = atom.workspace.getActiveTextEditor();
const providerRegistry = {
getProviderForEditor: () => {},
};
const definitions = [
{
language: 'Lang',
path: '/path/to/file',
position: { row: 0, column: 0 },
range: { start: { row: 0, column: 0 }, end: { row: 0, column: 1 } },
},
];
const word = 'foo';
const range = { start: { row: 0, column: 0 }, end: { row: 0, column: 3 } };

let clickProvider;
beforeEach(function() {
spyOn(atom.config, 'get').andCallFake(function(value) {
const config = {
'atom-ide-definitions.clickGrammarScopes': [],
'atom-ide-definitions.clickPriority': 0,
};
return config[value];
});

clickProvider = new ClickProvider(providerRegistry);
});

it('verify provider structure', () => {
const provider = clickProvider.getProvider();

expect(provider.priority).toEqual(0);
expect(provider.grammarScopes).toBeNull();
expect(typeof provider.getSuggestionForWord).toEqual('function');
});

it('getSuggestionForWord returns range and callback if definition is available', async () => {
const provider = clickProvider.getProvider();
spyOn(goToDefinition, 'getDefinitions').andCallFake(() => definitions);

const suggestionForWord = await provider.getSuggestionForWord(
editor,
word,
range
);

expect(suggestionForWord.range).toEqual(range);
expect(typeof suggestionForWord.callback).toEqual('function');
});

it('getSuggestionForWord returns false if definition is unavailable', async () => {
const provider = clickProvider.getProvider();
spyOn(goToDefinition, 'getDefinitions').andCallFake(() => null);

const suggestionForWord = await provider.getSuggestionForWord(
editor,
word,
range
);

expect(suggestionForWord).toEqual(false);
});
});

0 comments on commit e0200fa

Please sign in to comment.