Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Hal-9k1 committed Nov 28, 2024
1 parent e3de3b8 commit 7d4ff5c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 106 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import { config as aceConfig } from 'ace-builds';
import App from '../renderer/App';
import { config as aceConfig, require as acequire } from 'ace-builds';

describe('App', () => {
beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default function Editor({
};
useEffect(() => {
if (editorRef.current !== null) {
const editor = editorRef.current.editor;
const { editor } = editorRef.current;
addEditorAutocomplete(editor);
addEditorTooltips(editor);
}
Expand Down
142 changes: 68 additions & 74 deletions src/renderer/addEditorAutocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,42 @@ function makeContextCompleter(ctx: string, completions: string[]) {
_prefix: string,
callback: Ace.CompleterCallback,
) => {
const maxLength = ctx.length + Math.max(
...completions.map(completion => completion.length)
);
const maxLength =
ctx.length +
Math.max(...completions.map((completion) => completion.length));
const buf = readApiCall(session, pos, maxLength);
const isContext = buf.startsWith(ctx);
callback(null, isContext ? completions
.filter(completion => (ctx + completion).startsWith(buf.trim())
&& (ctx + completion) !== buf.trim())
.map(caption => {
return {
caption,
// FIXME
// Completion is multiple tokens (e.g. Gamepad.get_value completions): slice is needed
// to remove already-typed string from completion.
// Context is single token: unsliced is needed or else existing text is replaced with
// sliced completion, effectively deleting the already-typed bit.
//value: caption.slice(buf.length - ctx.length),
value: caption,
meta: 'PiE API',
score: COMP_SCORE,
};
}) : []
callback(
null,
isContext
? completions
.filter(
(completion) =>
(ctx + completion).startsWith(buf.trim()) &&
ctx + completion !== buf.trim(),
)
.map((caption) => {
return {
caption,
// FIXME
// Completion is multiple tokens (e.g. Gamepad.get_value completions): slice is needed
// to remove already-typed string from completion.
// Context is single token: unsliced is needed or else existing text is replaced with
// sliced completion, effectively deleting the already-typed bit.
// value: caption.slice(buf.length - ctx.length),
value: caption,
meta: 'PiE API',
score: COMP_SCORE,
};
})
: [],
);
},
onInsert: (
_editor: Ace.Editor,
completion: Ace.Completion,
) => {
onInsert: (_editor: Ace.Editor, _completion: Ace.Completion) => {
// Adding something over here could maybe fix sliced completion?
},
};
};
}

/**
* Wraps a completer so it does not trigger if the current or preceeding token contains a dot.
Expand All @@ -99,12 +103,14 @@ function adaptGlobalCompleter(completer: Ace.Completer) {
callback: Ace.CompleterCallback,
) => {
const iter = new TokenIterator(session, pos.row, pos.column);
const curTokenDot = iter.getCurrentToken() !== undefined
&& iter.getCurrentToken().value
&& iter.getCurrentToken().value.includes('.');
const prevTokenDot = iter.stepBackward() !== null
&& iter.getCurrentToken().value
&& iter.getCurrentToken().value.includes('.');
const curTokenDot =
iter.getCurrentToken() !== undefined &&
iter.getCurrentToken().value &&
iter.getCurrentToken().value.includes('.');
const prevTokenDot =
iter.stepBackward() !== null &&
iter.getCurrentToken().value &&
iter.getCurrentToken().value.includes('.');
if (!curTokenDot && !prevTokenDot) {
completer.getCompletions(editor, session, pos, prefix, callback);
}
Expand Down Expand Up @@ -141,51 +147,39 @@ export default function addEditorAutocomplete(editor: Ace.Editor) {
});
editor.completers = [
adaptGlobalCompleter(globalCompleter),
makeContextCompleter(
'Robot.',
[
'get_value',
'set_value',
'start_pos',
'sleep',
'log',
'is_running',
'run',
],
),
makeContextCompleter(
'Gamepad.',
['available', 'get_value'],
),
makeContextCompleter(
'Gamepad.get_value(',
[
'"button_a"',
'"button_b"',
'"button_x"',
'"button_y"',
'"l_bumper"',
'"r_bumper"',
'"l_trigger"',
'"r_trigger"',
'"button_back"',
'"button_start"',
'"l_stick"',
'"r_stick"',
'"dpad_up"',
'"dpad_down"',
'"dpad_left"',
'"dpad_right"',
'"button_xbox"',
],
),
makeContextCompleter(
'Keyboard.',
['available', 'get_value'],
),
makeContextCompleter('Robot.', [
'get_value',
'set_value',
'start_pos',
'sleep',
'log',
'is_running',
'run',
]),
makeContextCompleter('Gamepad.', ['available', 'get_value']),
makeContextCompleter('Gamepad.get_value(', [
'"button_a"',
'"button_b"',
'"button_x"',
'"button_y"',
'"l_bumper"',
'"r_bumper"',
'"l_trigger"',
'"r_trigger"',
'"button_back"',
'"button_start"',
'"l_stick"',
'"r_stick"',
'"dpad_up"',
'"dpad_down"',
'"dpad_left"',
'"dpad_right"',
'"button_xbox"',
]),
makeContextCompleter('Keyboard.', ['available', 'get_value']),
makeContextCompleter(
'Keyboard.get_value(',
Object.keys(robotKeyNumberMap).map(c => `"${c}"`),
Object.keys(robotKeyNumberMap).map((c) => `"${c}"`),
),
...(editor.completers || []).map(adaptGlobalCompleter),
];
Expand Down
16 changes: 4 additions & 12 deletions src/renderer/addEditorTooltips.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,12 @@ import { createRoot } from 'react-dom/client';
import readApiCall from './readApiCall';

const { HoverTooltip } = acequire('ace/tooltip');
const { TokenIterator } = acequire('ace/token_iterator');

const apiHelpComponents: {
[matchText: string]: () => ReactNode;
} = {
'Robot.get_value': () => (
<div>
Documentation for Robot.get_value method.
</div>
),
'Robot': () => (
<div>
Documentation for Robot object.
</div>
),
'Robot.get_value': () => <div>Documentation for Robot.get_value method.</div>,
Robot: () => <div>Documentation for Robot object.</div>,
};

/**
Expand All @@ -30,7 +21,8 @@ export default function addEditorTooltips(editor: Ace.Editor) {
const node = document.createElement('div');
const root = createRoot(node);
// Check just past longest match in case the very next character
const maxMatchTextLength = Math.max(...Object.keys(apiHelpComponents).map(s => s.length)) + 1;
const maxMatchTextLength =
Math.max(...Object.keys(apiHelpComponents).map((s) => s.length)) + 1;
tooltip.setDataProvider((event: any, _editor: Ace.Editor) => {
const pos: Ace.Position = event.getDocumentPosition();
const range = editor.session.getWordRange(pos.row, pos.column);
Expand Down
36 changes: 18 additions & 18 deletions src/renderer/readApiCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ const { TokenIterator } = acequire('ace/token_iterator');
* @return The text of the read "API call", which may be an empty string if the first token before
* pos is not part of an API call.
*/
export default (session: Ace.EditSession, pos: Ace.Position, minLength: number): string => {
export default (
session: Ace.EditSession,
pos: Ace.Position,
minLength: number,
): string => {
const iter = new TokenIterator(session, pos.row, pos.column);
let token = iter.getCurrentToken();
const tokenIsIdent = () => ['identifier', 'function.support'].includes(token.type);
const tokenIsIdent = () =>
['identifier', 'function.support'].includes(token.type);
const firstToken = token;
let isInterrupted = false;
while (token === undefined || token.value.trim() === '') {
isInterrupted = true;
token = iter.stepBackward();
if (token === null) {
return '';
Expand All @@ -33,9 +36,6 @@ export default (session: Ace.EditSession, pos: Ace.Position, minLength: number):
return '';
}
let lastWasIdentifier = tokenIsIdent();
if (iter.getCurrentTokenRow() !== pos.row) {
isInterrupted = true;
}
let buf = token.value.trim();
let posInBuf;
if (token === firstToken) {
Expand All @@ -51,20 +51,20 @@ export default (session: Ace.EditSession, pos: Ace.Position, minLength: number):
}
const tokenIsWhitespace = token.value.trim() === '';
// The following conditions cause a token break if coming before an identifier:
const preIdentBreak = tokenIsIdent()
|| token.type.startsWith('paren')
|| token.type === 'string'
|| (token.type === 'punctuation' && !token.value.includes('.'));
const preIdentBreak =
tokenIsIdent() ||
token.type.startsWith('paren') ||
token.type === 'string' ||
(token.type === 'punctuation' && !token.value.includes('.'));
if (lastWasIdentifier && preIdentBreak) {
break;
}
if (token.type === 'comment') {
continue;
}
buf = token.value.trim() + buf;
posInBuf += token.value.trim().length;
if (!tokenIsWhitespace) {
lastWasIdentifier = tokenIsIdent();
if (token.type !== 'comment') {
buf = token.value.trim() + buf;
posInBuf += token.value.trim().length;
if (!tokenIsWhitespace) {
lastWasIdentifier = tokenIsIdent();
}
}
}
return buf;
Expand Down

0 comments on commit 7d4ff5c

Please sign in to comment.