Skip to content

Commit fd3f5de

Browse files
committed
rebase
1 parent 0ab685a commit fd3f5de

File tree

9 files changed

+49439
-1298
lines changed

9 files changed

+49439
-1298
lines changed

client/modules/IDE/components/Editor.jsx

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import PropTypes from 'prop-types';
22
import React from 'react';
33
import CodeMirror from 'codemirror';
4+
import Fuse from 'fuse.js';
45
import emmet from '@emmetio/codemirror-plugin';
56
import prettier from 'prettier';
67
import babelParser from 'prettier/parser-babel';
@@ -27,6 +28,7 @@ import 'codemirror/addon/search/jump-to-line';
2728
import 'codemirror/addon/edit/matchbrackets';
2829
import 'codemirror/addon/edit/closebrackets';
2930
import 'codemirror/addon/selection/mark-selection';
31+
import 'codemirror/addon/hint/css-hint';
3032

3133
import { JSHINT } from 'jshint';
3234
import { CSSLint } from 'csslint';
@@ -41,6 +43,8 @@ import '../../../utils/webGL-clike';
4143
import Timer from '../components/Timer';
4244
import EditorAccessibility from '../components/EditorAccessibility';
4345
import { metaKey } from '../../../utils/metaKey';
46+
import './EditorShowHint';
47+
import * as hinter from '../../../utils/p5-hinter';
4448

4549
import '../../../utils/codemirror-search';
4650

@@ -126,6 +130,10 @@ class Editor extends React.Component {
126130
}
127131
}
128132
});
133+
this.hinter = new Fuse(hinter.p5Hinter, {
134+
threshold: 0.2,
135+
keys: ['text']
136+
});
129137

130138
delete this._cm.options.lint.options.errors;
131139

@@ -175,16 +183,21 @@ class Editor extends React.Component {
175183
});
176184

177185
this._cm.on('keydown', (_cm, e) => {
178-
// 70 === f
179186
if (
180187
((metaKey === 'Cmd' && e.metaKey) ||
181188
(metaKey === 'Ctrl' && e.ctrlKey)) &&
182189
e.shiftKey &&
183-
e.keyCode === 70
190+
e.key === 'f'
184191
) {
185192
e.preventDefault();
186193
this.tidyCode();
187194
}
195+
196+
// Show hint
197+
const mode = this._cm.getOption('mode');
198+
if (/^[a-z]$/i.test(e.key) && (mode === 'css' || mode === 'javascript')) {
199+
this.showHint(_cm);
200+
}
188201
});
189202

190203
this._cm.getWrapperElement().style[
@@ -317,6 +330,39 @@ class Editor extends React.Component {
317330
this._cm.execCommand('findPersistent');
318331
}
319332

333+
showHint(_cm) {
334+
const hintOptions = {
335+
completeSingle: false,
336+
completeOnSingleClick: false,
337+
closeOnUnfocus: false
338+
// closeOnPick: false
339+
};
340+
341+
// CSS
342+
if (_cm.options.mode === 'css') {
343+
CodeMirror.showHint(_cm, CodeMirror.hint.css, hintOptions);
344+
return;
345+
}
346+
347+
// JavaScript
348+
CodeMirror.showHint(
349+
_cm,
350+
() => {
351+
const c = _cm.getCursor();
352+
const token = _cm.getTokenAt(c);
353+
354+
const hints = this.hinter.search(token.string);
355+
356+
return {
357+
list: hints,
358+
from: CodeMirror.Pos(c.line, token.start),
359+
to: CodeMirror.Pos(c.line, c.ch)
360+
};
361+
},
362+
hintOptions
363+
);
364+
}
365+
320366
showReplace() {
321367
this._cm.execCommand('replace');
322368
}

0 commit comments

Comments
 (0)