Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for vim keymap and emulation #3044

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkgs/dartpad_ui/lib/editor/codemirror.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ extension type CodeMirror._(JSObject _) implements JSObject {
String getTheme() => (getOption('theme') as JSString).toDart;
void setTheme(String theme) => setOption('theme', theme.toJS);

String getKeymap() => (getOption('keyMap') as JSString).toDart;
void setKeymap(String keyMap) => setOption('keyMap', keyMap.toJS);

external void scrollTo(num? x, num? y);
external ScrollInfo getScrollInfo();

Expand Down
13 changes: 13 additions & 0 deletions pkgs/dartpad_ui/lib/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
appModel.sourceCodeController.addListener(_updateCodemirrorFromModel);
appModel.analysisIssues
.addListener(() => _updateIssues(appModel.analysisIssues.value));
appModel.vimKeymapsEnabled.addListener(_updateCodemirrorKeymap);

widget.appServices.registerEditorService(this);

Expand Down Expand Up @@ -317,6 +318,7 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
widget.appModel.sourceCodeController
.removeListener(_updateCodemirrorFromModel);
widget.appModel.appReady.removeListener(_updateEditableStatus);
widget.appModel.vimKeymapsEnabled.removeListener(_updateCodemirrorKeymap);

super.dispose();
}
Expand Down Expand Up @@ -432,6 +434,17 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
);
}
}

void _updateCodemirrorKeymap() {
final enabled = widget.appModel.vimKeymapsEnabled.value;
final cm = codeMirror!;

if (enabled) {
cm.setKeymap('vim');
} else {
cm.setKeymap('default');
}
}
}

// codemirror commands
Expand Down
37 changes: 36 additions & 1 deletion pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,10 @@ class StatusLineWidget extends StatelessWidget {
builder: (context) => MediumDialog(
title: 'Keyboard shortcuts',
smaller: true,
child: KeyBindingsTable(bindings: keys.keyBindings),
child: KeyBindingsTable(
bindings: keys.keyBindings,
appModel: appModel,
),
),
),
child: Icon(
Expand Down Expand Up @@ -1156,9 +1159,11 @@ class ContinueInMenu extends StatelessWidget {

class KeyBindingsTable extends StatelessWidget {
final List<(String, List<ShortcutActivator>)> bindings;
final AppModel appModel;

const KeyBindingsTable({
required this.bindings,
required this.appModel,
super.key,
});

Expand Down Expand Up @@ -1209,6 +1214,10 @@ class KeyBindingsTable extends StatelessWidget {
],
),
),
const Divider(),
_VimModeSwitch(
appModel: appModel,
),
],
);
}
Expand Down Expand Up @@ -1282,6 +1291,32 @@ class _BrightnessButton extends StatelessWidget {
}
}

class _VimModeSwitch extends StatelessWidget {
final AppModel appModel;

const _VimModeSwitch({
required this.appModel,
});

@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: appModel.vimKeymapsEnabled,
builder: (BuildContext context, bool value, Widget? child) {
return SwitchListTile(
value: value,
title: const Text('Use Vim Key Bindings'),
onChanged: _handleToggle,
);
},
);
}

void _handleToggle(bool value) {
appModel.vimKeymapsEnabled.value = value;
}
}

extension MenuControllerToggleMenu on MenuController {
void toggleMenuState() {
if (isOpen) {
Expand Down
2 changes: 2 additions & 0 deletions pkgs/dartpad_ui/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class AppModel {
final SplitDragStateManager splitDragStateManager = SplitDragStateManager();
late final StreamSubscription<SplitDragState> _splitSubscription;

final ValueNotifier<bool> vimKeymapsEnabled = ValueNotifier(false);

AppModel() {
consoleOutput.addListener(_recalcLayout);

Expand Down