xref: /aosp_15_r20/external/pigweed/pw_web/webconsole/components/repl/basicSetup.ts (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1// Copyright 2022 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15import {
16  keymap,
17  highlightSpecialChars,
18  drawSelection,
19  highlightActiveLine,
20  dropCursor,
21  rectangularSelection,
22  crosshairCursor,
23  highlightActiveLineGutter,
24} from '@codemirror/view';
25import { Extension, EditorState } from '@codemirror/state';
26import {
27  defaultHighlightStyle,
28  syntaxHighlighting,
29  indentOnInput,
30  bracketMatching,
31  foldGutter,
32  foldKeymap,
33} from '@codemirror/language';
34import { defaultKeymap, history, historyKeymap } from '@codemirror/commands';
35import { searchKeymap, highlightSelectionMatches } from '@codemirror/search';
36import {
37  autocompletion,
38  completionKeymap,
39  closeBrackets,
40  closeBracketsKeymap,
41} from '@codemirror/autocomplete';
42import { lintKeymap } from '@codemirror/lint';
43
44const defaultKeymapMinusEnterAndArrowUpDown = defaultKeymap.map((keymap) => {
45  if (keymap.key === 'Enter') {
46    return { ...keymap, key: 'Shift-Enter' };
47  }
48  if (keymap.key === 'ArrowUp') {
49    return { ...keymap, key: 'Shift-ArrowUp' };
50  }
51  if (keymap.key === 'ArrowDown') {
52    return { ...keymap, key: 'Shift-ArrowDown' };
53  }
54  return keymap;
55});
56
57export const basicSetup: Extension = (() => [
58  highlightActiveLineGutter(),
59  highlightSpecialChars(),
60  history(),
61  foldGutter(),
62  drawSelection(),
63  dropCursor(),
64  EditorState.allowMultipleSelections.of(true),
65  indentOnInput(),
66  syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
67  bracketMatching(),
68  closeBrackets(),
69  autocompletion(),
70  rectangularSelection(),
71  crosshairCursor(),
72  highlightActiveLine(),
73  highlightSelectionMatches(),
74  keymap.of([
75    ...closeBracketsKeymap,
76    ...defaultKeymapMinusEnterAndArrowUpDown,
77    ...searchKeymap,
78    ...historyKeymap,
79    ...foldKeymap,
80    ...completionKeymap,
81    ...lintKeymap,
82  ]),
83])();
84