-
Notifications
You must be signed in to change notification settings - Fork 217
Improve 3-backtick to insert code blocks #1493
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,77 @@ | ||
| import { EditorSelection } from '@codemirror/state'; | ||
| import { EditorSelection, EditorState } from '@codemirror/state'; | ||
| import { EditorView } from '@codemirror/view'; | ||
| import { syntaxTree } from '@codemirror/language'; | ||
| import { SyntaxNode } from '@lezer/common'; | ||
| import insertSnippet from '../snippets/insertSnippet'; | ||
|
|
||
| const mark = '`'; | ||
| const pair = mark.repeat(2); | ||
| const fence = mark.repeat(3); | ||
|
|
||
| /** | ||
| * When backtick key is detected, try inserting a code block if we already have two backticks. | ||
| */ | ||
| export default function insertCodeBlock(editor: EditorView) { | ||
| const state = editor.state; | ||
| const doc = state.doc; | ||
| const mark = '`'; | ||
| const prefix = mark + state.lineBreak; | ||
|
|
||
| editor.dispatch(state.changeByRange(({ from, to }) => { | ||
| if (doc.sliceString(from - 2, from) !== `${mark}${mark}`) { | ||
| // Fallback to inserting only one backtick | ||
| return { | ||
| range: EditorSelection.cursor(from + mark.length), | ||
| changes: { from, to, insert: mark }, | ||
| }; | ||
| } | ||
| const { from } = state.selection.main; | ||
|
|
||
| // Insert a code block, always placing it on its own lines | ||
| if (shouldInsertCodeBlock(state, from)) { | ||
|
cyanzhong marked this conversation as resolved.
|
||
| const line = doc.lineAt(from); | ||
| const lineBreak = state.lineBreak; | ||
|
|
||
| // Break out surrounding text so the fences never share a line with other content | ||
| const leading = doc.sliceString(line.from, from - 2).trim() === '' ? '' : lineBreak; | ||
| const trailing = doc.sliceString(from, line.to).trim() === '' ? '' : lineBreak; | ||
|
|
||
| // Replace the two existing backticks so the opening fence can start on its own line | ||
| insertSnippet( | ||
| `${leading}${fence}#{}${lineBreak}#{}${lineBreak}${fence}${trailing}`, | ||
| '', | ||
| { from: from - 2, to: from }, | ||
| ); | ||
|
|
||
| // Insert an empty code block and move the cursor to the empty line | ||
| return { | ||
| range: EditorSelection.cursor(from + mark.length + 1), // Don't use prefix.length, it doesn't work for CRLF | ||
| changes: { | ||
| from, to, insert: prefix + `${state.lineBreak}${mark}${mark}${mark}`, | ||
| }, | ||
| }; | ||
| })); | ||
| return true; | ||
|
cyanzhong marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Fallback to inserting only one backtick | ||
| editor.dispatch(state.changeByRange(({ from, to }) => ({ | ||
| range: EditorSelection.cursor(from + mark.length), | ||
| changes: { from, to, insert: mark }, | ||
| }))); | ||
|
|
||
| // Intercepted, default behavior is ignored | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * A code block is inserted when two backticks precede an empty cursor and we are not already inside code. | ||
| */ | ||
| function shouldInsertCodeBlock(state: EditorState, pos: number) { | ||
| // Only expand for a single empty selection with room for two preceding backticks. | ||
| // Multiple ranges fall through to the per-range fallback so the backtick isn't dropped. | ||
| if (pos < 2 || state.selection.ranges.length > 1 || !state.selection.main.empty) { | ||
| return false; | ||
| } | ||
|
|
||
| // Requires exactly two backticks right before the cursor | ||
| const doc = state.doc; | ||
| if (doc.sliceString(pos - 2, pos) !== pair) { | ||
| return false; | ||
| } | ||
|
|
||
| // Don't start a new block when the cursor is already inside code | ||
| for (let node: SyntaxNode | null = syntaxTree(state).resolveInner(pos, -1); node !== null; node = node.parent) { | ||
| if (node.name === 'FencedCode' || node.name === 'CodeBlock') { | ||
| return false; | ||
| } | ||
|
|
||
| // Ignore an inline span that our just-typed backticks would open, only bail when truly inside one | ||
| if (node.name === 'InlineCode' && node.from < pos - 2) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.