handle method
KeyEventResult
handle({
- required AffogatoAPI api,
- required EditorKeyEvent editorKeyEvent,
override
Implementation
@override
KeyEventResult handle({
required AffogatoAPI api,
required EditorKeyEvent editorKeyEvent,
}) {
if (!editorKeyEvent.editingContext.selection.isCollapsed ||
editorKeyEvent.editingContext.selection.start - 1 < 0) {
return KeyEventResult.ignored;
}
if (editorKeyEvent.keyEvent is KeyDownEvent) {
final String prevChar = editorKeyEvent.editingContext
.content[editorKeyEvent.editingContext.selection.start - 1];
final _IndentType indentType = triggerChars.containsKey(prevChar)
? _IndentType.add
: editorKeyEvent.keyEvent.logicalKey == LogicalKeyboardKey.enter
? _IndentType.maintain
: _IndentType.none;
if (indentType == _IndentType.add || indentType == _IndentType.maintain) {
final String precedentText = editorKeyEvent.editingContext.selection
.textBefore(editorKeyEvent.editingContext.content);
final int numSpaces =
numSpacesBeforeFirstChar(precedentText.split('\n').last);
if (numSpaces == 0 && indentType == _IndentType.maintain) {
return KeyEventResult.ignored;
}
final int tabSizeInSpaces =
api.workspace.workspaceConfigs.stylingConfigs.tabSizeInSpaces;
final String insertText =
"\n${' ' * (indentType == _IndentType.add ? (numSpaces + tabSizeInSpaces) : 0)}${indentType == _IndentType.add ? '\n' : ''}${' ' * numSpaces}";
final String succeedingText = editorKeyEvent.editingContext.selection
.textAfter(editorKeyEvent.editingContext.content);
final String newText = precedentText + insertText + succeedingText;
api.vfs.documentRequestChange(
VFSDocumentRequestChangeEvent(
originId: name,
entityId: api.workspace.workspaceConfigs.entitiesLocation.entries
.firstWhere(
(entry) => entry.value.$1 == editorKeyEvent.instanceId)
.key,
editorAction: EditorAction(
editingValue: TextEditingValue(
text: newText,
selection: TextSelection.collapsed(
offset: precedentText.length +
1 +
numSpaces +
(indentType == _IndentType.add
? api.workspace.workspaceConfigs.stylingConfigs
.tabSizeInSpaces
: 0),
),
),
),
),
);
return KeyEventResult.handled;
}
} else if (editorKeyEvent.keyEvent is KeyRepeatEvent) {}
return KeyEventResult.ignored;
}