codeHandleClosingDelimiterAlignment function

TextCommandResult codeHandleClosingDelimiterAlignment({
  1. required TextDocument document,
  2. required TextOffsetStateSnapshot state,
  3. required CodeLanguageProfile profile,
  4. required String typed,
  5. required int indentWidth,
})

Implementation

TextCommandResult codeHandleClosingDelimiterAlignment({
  required TextDocument document,
  required TextOffsetStateSnapshot state,
  required CodeLanguageProfile profile,
  required String typed,
  required int indentWidth,
}) {
  if (state.hasSelection || !profile.closingToOpening.containsKey(typed)) {
    return _unchangedCodeResultFromDocument(document, state);
  }

  final cursor = document.positionForOffset(state.cursorOffset);
  final lineIndex = cursor.line.clamp(0, document.lineCount - 1);
  final lineGraphemes = document.lineGraphemesAt(lineIndex);
  final beforeCursor = lineGraphemes.take(cursor.column).join();
  final line = lineGraphemes.join();
  if (beforeCursor.trim().isNotEmpty || line.trim().isNotEmpty) {
    return _unchangedCodeResultFromDocument(document, state);
  }

  final nextIndent = codeOutdentedIndent(beforeCursor, indentWidth);
  return textTransformSelectionOrLine(
    document: document,
    state: state,
    transform: (_) => '$nextIndent$typed',
  );
}