Skip to content

Commit

Permalink
Merge branch 'jupyterlab-v0.35'
Browse files Browse the repository at this point in the history
krassowski committed Mar 16, 2019

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
2 parents 5700a4b + d3db152 commit 84fda7d
Showing 5 changed files with 103 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -8,13 +8,17 @@ Use <kbd>Alt</kbd> + <kbd>click</kbd> to jump to a definition using your mouse,

![Go to definition](https://raw.githubusercontent.com/krassowski/jupyterlab-go-to-definition/master/examples/demo.gif)

You can replace the key modifier for mouse click from <kbd>Alt</kbd> to <kbd>Control</kbd>, <kbd>Shift</kbd>, <kbd>Meta</kbd> or <kbd>AltGraph</kbd> in the settings.
You can replace the key modifier for mouse click from <kbd>Alt</kbd> to <kbd>Control</kbd>, <kbd>Shift</kbd>, <kbd>Meta</kbd> or <kbd>AltGraph</kbd> in the settings*.

To jump back to the variable/function usage, use <kbd>Alt</kbd> + <kbd>o</kbd>.

The plugin is language-agnostic, though optimized for Python. Initial support for R was recently implemented.
Support for other languages is possible (PRs welcome).

*) For full list of physical keys mapped to the modifiers (which depend on your Operating System), please see [the MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState).

Safari users: Safari does not implement `MouseEvent.getModifierState` (see [#3](https://github.com/krassowski/jupyterlab-go-to-definition/issues/3)), thus only <kbd>Alt</kbd>, <kbd>Control</kbd>, <kbd>Shift</kbd> and <kbd>Meta</kbd> are supported.

## Prerequisites

* JupyterLab
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@krassowski/jupyterlab_go_to_definition",
"version": "0.1.4",
"version": "0.1.6",
"description": "Jump to definition of a variable or function in JupyterLab",
"keywords": [
"jupyter",
27 changes: 26 additions & 1 deletion src/editors/codemirror/extension.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,31 @@ import { CodeMirrorTokensProvider } from "./tokens";
const HANDLERS_ON = '_go_to_are_handlers_on';


function getModifierState(event: MouseEvent, modifierKey: string): boolean {
// Note: Safari does not support getModifierState on MouseEvent, see:
// https://github.com/krassowski/jupyterlab-go-to-definition/issues/3
// thus AltGraph and others are not supported on Safari
// Full list of modifier keys and mappings to physical keys on different OSes:
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState

if (event.getModifierState !== undefined) {
return event.getModifierState(modifierKey)
}

switch (modifierKey) {
case 'Shift':
return event.shiftKey;
case 'Alt':
return event.altKey;
case 'Control':
return event.ctrlKey;
case 'Meta':
return event.metaKey;
}
}



export class CodeMirrorExtension extends CodeMirrorTokensProvider implements IEditorExtension {

jumper: CodeJumper;
@@ -44,7 +69,7 @@ export class CodeMirrorExtension extends CodeMirrorTokensProvider implements IEd
//codemirror_editor.addKeydownHandler()
let target = event.target as HTMLElement;
const {button} = event;
if (button === 0 && event.getModifierState(CodeMirrorExtension.modifierKey as string)) {
if (button === 0 && getModifierState(event, CodeMirrorExtension.modifierKey as string)) {

const classes = ['cm-variable', 'cm-property'];

23 changes: 23 additions & 0 deletions src/languages/python.spec.ts
Original file line number Diff line number Diff line change
@@ -64,6 +64,29 @@ describe('PythonAnalyzer', () => {
});
});

describe('#isStoreMagic()', () => {

it('should recognize IPython %store -r magic function', () => {
model.value.text = '%store -r x';
expect(runWithSelectedToken(analyzer.isStoreMagic, 'x')).to.be.true;
});

it('should ignore other look-alikes', () => {
model.value.text = '%store x';
expect(runWithSelectedToken(analyzer.isStoreMagic, 'x')).to.be.false;

model.value.text = '%store -r xx';
expect(runWithSelectedToken(analyzer.isStoreMagic, 'x')).to.be.false;

model.value.text = 'store -r x';
expect(runWithSelectedToken(analyzer.isStoreMagic, 'x')).to.be.false;

model.value.text = '# %store -r x';
expect(runWithSelectedToken(analyzer.isStoreMagic, 'x')).to.be.false;
});
});


describe('#isTupleUnpacking', () => {
it('should recognize simple tuples', () => {
model.value.text = 'a, b = 1, 2';
49 changes: 48 additions & 1 deletion src/languages/python.ts
Original file line number Diff line number Diff line change
@@ -52,15 +52,62 @@ export class PythonAnalyzer extends LanguageWithOptionalSemicolons {
this.isImport,
this.isWithStatement,
this.isForLoopOrComprehension,
this.isTupleUnpacking
this.isTupleUnpacking,
this.isStoreMagic
];

// Matching standalone variable assignment:
isStandaloneAssignment(siblings: IMeaningfulSiblings) {
let { next } = siblings;

return next && this.isAssignment(next)
}

// IPython %store -r magic:
isStoreMagic(siblings: IMeaningfulSiblings, tokens: ReadonlyArray<IToken>, i: number) {
// this may be much better using regexpr
// (and more compatible with other engines, but possibly slower)
let { previous } = siblings;

if (previous && previous.value === 'r') {
let r_pos = i - 2;
let has_r_switch = false;

while (i - r_pos < 10 && r_pos >= 3) {

if (tokens[r_pos].value === 'r') {
if (tokens[r_pos - 1].value === '-') {
has_r_switch = true;
}
break;
}
if (tokens[r_pos].value.trim() != '') {
return false;
}
r_pos -= 1;

}
if (!has_r_switch) {
return false;
}

let store_pos = r_pos - 2;
while (r_pos - store_pos < 10 && store_pos >= 1) {
if (tokens[store_pos].value === 'store') {
if (tokens[store_pos - 1].value === '%') {
return true;
}
}
if (tokens[store_pos].value.trim() != '') {
return false;
}
store_pos -= 1;
}
return false;
}
return false;
}

// Matching imports:
isImport(siblings: IMeaningfulSiblings) {
let { previous } = siblings;

0 comments on commit 84fda7d

Please sign in to comment.