From beacfdd0c989634787534528a25cea99309b9cfb Mon Sep 17 00:00:00 2001 From: anijanyan Date: Wed, 8 Apr 2026 16:36:00 +0400 Subject: [PATCH] fix: Enhance bracket indentation handling for square brackets --- src/mode/behaviour/behaviour_test.js | 10 +++++++++- src/mode/behaviour/cstyle.js | 21 +++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/mode/behaviour/behaviour_test.js b/src/mode/behaviour/behaviour_test.js index 44cca9c4763..6072e919da8 100644 --- a/src/mode/behaviour/behaviour_test.js +++ b/src/mode/behaviour/behaviour_test.js @@ -96,7 +96,15 @@ module.exports = { assert.equal(editor.getValue(), "{"); exec("insertstring", 1, "\n"); assert.equal(editor.getValue(), "{\n \n}"); - + + //test bracket indentation + editor.setValue(""); + exec("insertstring", 1, " "); + exec("insertstring", 1, "["); + assert.equal(editor.getValue(), " []"); + exec("insertstring", 1, "\n"); + assert.equal(editor.getValue(), " [\n \n ]"); + editor.setValue(""); exec("insertstring", 1, "("); exec("insertstring", 1, '"'); diff --git a/src/mode/behaviour/cstyle.js b/src/mode/behaviour/cstyle.js index 9af7ce17c9b..6dcacb00616 100644 --- a/src/mode/behaviour/cstyle.js +++ b/src/mode/behaviour/cstyle.js @@ -196,6 +196,8 @@ CstyleBehaviour = function(options) { }); this.add("brackets", "insertion", function(state, action, editor, session, text) { + var cursor = editor.getCursorPosition(); + var line = session.doc.getLine(cursor.row); if (text == '[') { initContext(editor); var selection = editor.getSelectionRange(); @@ -211,8 +213,6 @@ CstyleBehaviour = function(options) { } } else if (text == ']') { initContext(editor); - var cursor = editor.getCursorPosition(); - var line = session.doc.getLine(cursor.row); var rightChar = line.substring(cursor.column, cursor.column + 1); if (rightChar == ']') { var matching = session.$findOpeningBracket(']', {column: cursor.column + 1, row: cursor.row}); @@ -224,6 +224,23 @@ CstyleBehaviour = function(options) { }; } } + } else if (text == "\n" || text == "\r\n") { + initContext(editor); + var rightChar = line.substring(cursor.column, cursor.column + 1); + if (rightChar === ']') { + var openBracePos = session.findMatchingBracket({row: cursor.row, column: cursor.column + 1}, ']'); + if (!openBracePos) + return null; + var next_indent = this.$getIndent(session.getLine(openBracePos.row)); + } else { + return; + } + var indent = next_indent + session.getTabString(); + + return { + text: '\n' + indent + '\n' + next_indent, + selection: [1, indent.length, 1, indent.length] + }; } });