overleaf/services/web/frontend/js/features/source-editor/utils/prepare-lines.ts
Domagoj Kriskovic a05c201652 [cm6] indent figure modal generated code (#13213)
* [cm6] indent figure modal generated code

* fix: prettier

* fix indenting issues

* add licence on top

* fix cypress tests

GitOrigin-RevId: 8f74be537f19c2a29de3c742a9bbabe43b1ce40d
2023-06-01 08:03:49 +00:00

33 lines
995 B
TypeScript

/**
* Adapted from CodeMirror 6 (@codemirror/autocomplete), licensed under the MIT license:
* https://github.com/codemirror/autocomplete/blob/08f63add9f470a032d3802a4599caa86c75de5cb/src/snippet.ts#L29-L45
*/
import { indentUnit } from '@codemirror/language'
import { EditorState } from '@codemirror/state'
// apply correct indentation to passed lines
export function prepareLines(
lines: (string | null)[],
state: EditorState,
pos: number
) {
const text = []
const lineStart = [pos]
const lineObj = state.doc.lineAt(pos)
const baseIndent = /^\s*/.exec(lineObj.text)![0]
for (let line of lines) {
if (line === null) continue
if (text.length) {
let indent = baseIndent
const tabs = /^\t*/.exec(line)![0].length
for (let i = 0; i < tabs; i++) indent += state.facet(indentUnit)
lineStart.push(pos + indent.length - tabs)
line = indent + line.slice(tabs)
}
text.push(line)
pos += line.length + 1
}
return text.join('\n')
}