Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
fix(editor): preserve selected code when indenting (#34970)
OrcaWin1607c647cd5e
diff --git a/src/renderer/src/components/editor/rich-markdown-code-block-indent.test.ts b/src/renderer/src/components/editor/rich-markdown-code-block-indent.test.tsnew file mode 100644index 00000000000..0c966749935--- /dev/null+++ b/src/renderer/src/components/editor/rich-markdown-code-block-indent.test.ts@@ -0,0 +1,57 @@+// @vitest-environment happy-dom+import { afterEach, describe, expect, it } from 'vitest'+import { Editor } from '@tiptap/core'+import { createRichMarkdownExtensions } from './rich-markdown-extensions'+import { createRichMarkdownEditorCodec } from './rich-markdown-source-transport'+import { indentRichMarkdownCodeBlock } from './rich-markdown-code-block-indent'++let editor: Editor | null = null++function createMarkdownEditor(markdown: string): Editor {+ editor = new Editor({+ element: document.createElement('div'),+ extensions: createRichMarkdownExtensions({ codec: createRichMarkdownEditorCodec() }),+ content: markdown,+ contentType: 'markdown'+ })+ return editor+}++function codeBlockPosition(target: Editor, text: string): number {+ let position = -1+ target.state.doc.descendants((node, pos) => {+ if (node.type.name === 'codeBlock' && node.textContent.includes(text)) {+ position = pos + 1 + node.textContent.indexOf(text)+ }+ return position === -1+ })+ return position+}++afterEach(() => {+ editor?.destroy()+ editor = null+})++describe('indentRichMarkdownCodeBlock', () => {+ it('indents selected code lines without replacing their text, undoable in one step', () => {+ const target = createMarkdownEditor('```js\nconst a = 1\nconst b = 2\nconst c = 3\n```')+ target.commands.setTextSelection({+ from: codeBlockPosition(target, 'const a'),+ to: codeBlockPosition(target, 'const b') + 'const b = 2'.length+ })++ expect(indentRichMarkdownCodeBlock(target)).toBe(true)+ expect(target.getMarkdown()).toBe('```js\n const a = 1\n const b = 2\nconst c = 3\n```')+ expect(target.commands.undo()).toBe(true)+ expect(target.getMarkdown()).toBe('```js\nconst a = 1\nconst b = 2\nconst c = 3\n```')+ })++ it('inserts spaces at the caret for an empty selection', () => {+ const target = createMarkdownEditor('```js\nconst x = 1\n```')+ target.commands.setTextSelection(codeBlockPosition(target, 'x'))++ expect(indentRichMarkdownCodeBlock(target)).toBe(true)+ expect(target.getMarkdown()).toBe('```js\nconst x = 1\n```')+ })+})diff --git a/src/renderer/src/components/editor/rich-markdown-code-block-indent.ts b/src/renderer/src/components/editor/rich-markdown-code-block-indent.tsindex aa7458ed7ca..3b68ed7e3ca 100644--- a/src/renderer/src/components/editor/rich-markdown-code-block-indent.ts+++ b/src/renderer/src/components/editor/rich-markdown-code-block-indent.ts@@ -1,44 +1,63 @@import type { Editor } from '@tiptap/react'-export const RICH_MARKDOWN_CODE_BLOCK_INDENT = ' '+const RICH_MARKDOWN_CODE_BLOCK_INDENT = ' 'const LEADING_INDENT = /^(\t| {1,2})/-function findCodeBlockAtCursor(editor: Editor): { text: string; start: number } | null {- const { $from } = editor.state.selection- for (let depth = $from.depth; depth > 0; depth--) {- const node = $from.node(depth)- if (node.type.name === 'codeBlock') {- return { text: node.textContent, start: $from.start(depth) }+function changeCodeBlockIndent(editor: Editor, direction: 'indent' | 'outdent'): boolean {+ const { from, to, empty } = editor.state.selection+ const tr = editor.state.tr+ editor.state.doc.nodesBetween(from, to, (node, position) => {+ if (node.type.name !== 'codeBlock') {+ return true}+ let lineStart = position + 1+ for (const line of node.textContent.split('\n')) {+ const lineEnd = lineStart + line.length+ const isTouched = lineEnd >= from && (empty ? lineStart <= to : lineStart < to)+ if (isTouched) {+ const start = tr.mapping.map(lineStart)+ if (direction === 'indent') {+ tr.insertText(RICH_MARKDOWN_CODE_BLOCK_INDENT, start)+ } else {+ const indent = LEADING_INDENT.exec(line)+ if (indent) {+ tr.delete(start, tr.mapping.map(lineStart + indent[0].length))+ }+ }+ }+ lineStart = lineEnd + 1+ }+ return false+ })+ if (!tr.docChanged) {+ return false}- return null+ editor.view.dispatch(tr)+ return true}-/** Outdent each selected code line by one step, mirroring Tab. */-export function outdentRichMarkdownCodeBlock(editor: Editor): boolean {- const codeBlock = findCodeBlockAtCursor(editor)- if (!codeBlock) {- return false+export function indentRichMarkdownCodeBlock(editor: Editor): boolean {+ if (editor.state.selection.empty) {+ return editor.commands.insertContent(RICH_MARKDOWN_CODE_BLOCK_INDENT)}+ return changeCodeBlockIndent(editor, 'indent')+}- const { from, to } = editor.state.selection- const tr = editor.state.tr- let lineStart = codeBlock.start-- for (const line of codeBlock.text.split('\n')) {- const lineEnd = lineStart + line.length- const isTouched = lineEnd >= from && lineStart <= to- const indent = isTouched ? LEADING_INDENT.exec(line) : null- if (indent) {- tr.delete(tr.mapping.map(lineStart), tr.mapping.map(lineStart + indent[0].length))+function selectionStartsInCodeBlock(editor: Editor): boolean {+ const { $from } = editor.state.selection+ for (let depth = $from.depth; depth > 0; depth--) {+ if ($from.node(depth).type.name === 'codeBlock') {+ return true}- lineStart = lineEnd + 1}+ return false+}- if (!tr.docChanged) {+export function outdentRichMarkdownCodeBlock(editor: Editor): boolean {+ // Selections that start outside code (e.g. list items wrapping a code block) belong to list outdent.+ if (!selectionStartsInCodeBlock(editor) && !editor.isActive('codeBlock')) {return false}- editor.view.dispatch(tr)- return true+ return changeCodeBlockIndent(editor, 'outdent')}diff --git a/src/renderer/src/components/editor/rich-markdown-key-handler.ts b/src/renderer/src/components/editor/rich-markdown-key-handler.tsindex d22fd3157ef..be28eb08fb5 100644--- a/src/renderer/src/components/editor/rich-markdown-key-handler.ts+++ b/src/renderer/src/components/editor/rich-markdown-key-handler.ts@@ -27,8 +27,8 @@ import {outdentRichMarkdownListItem} from './rich-markdown-list-indent'import {- outdentRichMarkdownCodeBlock,- RICH_MARKDOWN_CODE_BLOCK_INDENT+ indentRichMarkdownCodeBlock,+ outdentRichMarkdownCodeBlock} from './rich-markdown-code-block-indent'import { handleRichMarkdownCitationKey } from './rich-markdown-citation-keyboard'import type { RichMarkdownHtmlSuperscriptLinkContext } from './rich-markdown-html-superscript-link-context'@@ -215,7 +215,7 @@ export function createRichMarkdownKeyHandler(}if (ed.isActive('codeBlock')) {- ed.commands.insertContent(RICH_MARKDOWN_CODE_BLOCK_INDENT)+ indentRichMarkdownCodeBlock(ed)return true}diff --git a/src/renderer/src/components/editor/rich-markdown-tab-key-handler.test.ts b/src/renderer/src/components/editor/rich-markdown-tab-key-handler.test.tsindex fad1c295b64..c6eec4ffb27 100644--- a/src/renderer/src/components/editor/rich-markdown-tab-key-handler.test.ts+++ b/src/renderer/src/components/editor/rich-markdown-tab-key-handler.test.ts@@ -438,6 +438,108 @@ describe('rich markdown Tab key handler', () => {}})+ it('keeps the line at the exclusive selection end untouched when outdenting code', () => {+ const editor = createMarkdownEditor('```js\n one\n two\n three\n```')++ try {+ editor.commands.setTextSelection({+ from: codeBlockPosition(editor, 'one'),+ to: codeBlockPosition(editor, 'three') - 2+ })++ expect(+ createRichMarkdownKeyHandler(createContext(editor))(+ null,+ keyEvent('Tab', { shiftKey: true })+ )+ ).toBe(true)+ expect(editor.getMarkdown()).toBe('```js\none\ntwo\n three\n```')+ } finally {+ editor.destroy()+ }+ })++ it('indents a selected fragment of one code line without deleting it', () => {+ const editor = createMarkdownEditor('```js\nconst value = 1\n```')++ try {+ const from = codeBlockPosition(editor, 'value')+ editor.commands.setTextSelection({ from, to: from + 'value'.length })++ expect(createRichMarkdownKeyHandler(createContext(editor))(null, keyEvent('Tab'))).toBe(true)+ expect(editor.getMarkdown()).toBe('```js\n const value = 1\n```')+ expect(+ editor.state.doc.textBetween(editor.state.selection.from, editor.state.selection.to)+ ).toBe('value')+ } finally {+ editor.destroy()+ }+ })++ it('outdents a selection spanning adjacent code blocks', () => {+ const editor = createMarkdownEditor('```ts\n first\n```\n\n```sh\n second\n```')+ try {+ const from = codeBlockPosition(editor, ' first')+ const to = codeBlockPosition(editor, ' second') + ' second'.length+ editor.commands.setTextSelection({ from, to })+ const handler = createRichMarkdownKeyHandler(createContext(editor))++ expect(handler(null, keyEvent('Tab', { shiftKey: true }))).toBe(true)+ const blocks: string[] = []+ editor.state.doc.descendants((node) => {+ if (node.type.name === 'codeBlock') {+ blocks.push(node.textContent)+ }+ })+ expect(blocks).toEqual(['first', 'second'])+ } finally {+ editor.destroy()+ }+ })++ it('outdents list items on Shift-Tab even when they contain an indented code block', () => {+ const markdown = '- Alpha\n - Beta\n\n ```js\n nested\n ```'+ const editor = createMarkdownEditor(markdown)++ try {+ editor.commands.setTextSelection({+ from: textPosition(editor, 'Beta'),+ to: codeBlockPosition(editor, 'nested') + 'nested'.length+ })++ expect(+ createRichMarkdownKeyHandler(createContext(editor))(+ null,+ keyEvent('Tab', { shiftKey: true })+ )+ ).toBe(true)+ expect(editor.getMarkdown()).toBe('- Alpha\n- Beta\n ```js\n nested\n ```')+ } finally {+ editor.destroy()+ }+ })++ it('outdents code lines when the selection starts in code and extends into a paragraph', () => {+ const editor = createMarkdownEditor('```js\n code\n```\n\nafter')++ try {+ editor.commands.setTextSelection({+ from: codeBlockPosition(editor, 'code'),+ to: textPosition(editor, 'after') + 2+ })++ expect(+ createRichMarkdownKeyHandler(createContext(editor))(+ null,+ keyEvent('Tab', { shiftKey: true })+ )+ ).toBe(true)+ expect(editor.getMarkdown()).toBe('```js\ncode\n```\n\nafter')+ } finally {+ editor.destroy()+ }+ })+it('leaves an unindented code block untouched on Shift-Tab', () => {const editor = createMarkdownEditor('```js\nconst x = 1\n```')@@ -452,24 +554,6 @@ describe('rich markdown Tab key handler', () => {}})- it('inserts spaces for Tab in code blocks', () => {- const insertContent = vi.fn()- const editor = {- view: { composing: false },- state: { selection: { $from: { depth: 0 } } },- commands: {- sinkListItem: vi.fn(),- liftListItem: vi.fn(),- insertContent- },- isActive: vi.fn((name) => name === 'codeBlock')- } as unknown as Editor- const event = keyEvent('Tab')-- expect(createRichMarkdownKeyHandler(createContext(editor))(null, event)).toBe(true)- expect(insertContent).toHaveBeenCalledWith(' ')- })-it('outdents a nested bullet that arrived as a pasted list node tree', () => {const editor = createNodeEditor(doc(bullets([para('One'), bullets([para('Two')])])))