line-wrapping-indentation.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {
  2. Decoration,
  3. DecorationSet,
  4. EditorView,
  5. ViewPlugin,
  6. ViewUpdate,
  7. } from '@codemirror/view'
  8. import { type Range, StateEffect, StateField } from '@codemirror/state'
  9. import { sourceOnly } from './visual/visual'
  10. const MAX_INDENT_FRACTION = 0.9
  11. const setMaxIndentEffect = StateEffect.define<number>()
  12. export const lineWrappingIndentation = (visual: boolean) => {
  13. // this extension should only be active in the source editor
  14. return sourceOnly(visual, [
  15. // store the current maxIndent, based on the clientWidth
  16. StateField.define<number>({
  17. create() {
  18. return 0
  19. },
  20. update(value, tr) {
  21. for (const effect of tr.effects) {
  22. if (effect.is(setMaxIndentEffect)) {
  23. value = effect.value
  24. }
  25. }
  26. return value
  27. },
  28. provide(field) {
  29. return [
  30. // calculate the max indent when the geometry changes
  31. ViewPlugin.define(view => {
  32. const measure = {
  33. key: 'line-wrapping-indentation-max-indent',
  34. read(view: EditorView) {
  35. return (
  36. (view.contentDOM.clientWidth / view.defaultCharacterWidth) *
  37. MAX_INDENT_FRACTION
  38. )
  39. },
  40. write(value: number, view: EditorView) {
  41. if (view.state.field(field) !== value) {
  42. window.setTimeout(() => {
  43. view.dispatch({
  44. effects: setMaxIndentEffect.of(value),
  45. })
  46. })
  47. }
  48. },
  49. }
  50. return {
  51. update(update: ViewUpdate) {
  52. if (update.geometryChanged) {
  53. view.requestMeasure(measure)
  54. }
  55. },
  56. }
  57. }),
  58. // rebuild the decorations when needed
  59. ViewPlugin.define<{ decorations: DecorationSet }>(
  60. view => {
  61. let previousMaxIndent = 0
  62. const value = {
  63. decorations: buildDecorations(view, view.state.field(field)),
  64. update(update: ViewUpdate) {
  65. const maxIndent = view.state.field(field)
  66. if (
  67. maxIndent !== previousMaxIndent ||
  68. update.geometryChanged ||
  69. update.viewportChanged
  70. ) {
  71. value.decorations = buildDecorations(view, maxIndent)
  72. }
  73. previousMaxIndent = maxIndent
  74. },
  75. }
  76. return value
  77. },
  78. {
  79. decorations: value => value.decorations,
  80. }
  81. ),
  82. ]
  83. },
  84. }),
  85. ])
  86. }
  87. export const buildDecorations = (view: EditorView, maxIndent: number) => {
  88. const { state } = view
  89. const { doc, tabSize } = state
  90. const decorations: Range<Decoration>[] = []
  91. let from = 0
  92. for (const line of doc.iterLines()) {
  93. // const indent = line.match(/^(\s*)/)[1].length
  94. const indent = calculateIndent(line, tabSize, maxIndent)
  95. if (indent) {
  96. decorations.push(lineIndentDecoration(indent).range(from))
  97. }
  98. from += line.length + 1
  99. }
  100. return Decoration.set(decorations)
  101. }
  102. const lineIndentDecoration = (indent: number) =>
  103. Decoration.line({
  104. attributes: {
  105. // style: `text-indent: ${indent}ch hanging`, // "hanging" would be ideal, when browsers support it
  106. style: `text-indent: -${indent}ch; padding-left: calc(${indent}ch + 6px)`, // add 6px to account for existing padding-left
  107. },
  108. })
  109. // calculate the character width of whitespace at the start of a line
  110. const calculateIndent = (line: string, tabSize: number, maxIndent: number) => {
  111. let indent = 0
  112. for (const char of line) {
  113. if (char === ' ') {
  114. indent++
  115. } else if (char === '\t') {
  116. indent += tabSize - (indent % tabSize)
  117. } else {
  118. break
  119. }
  120. }
  121. return Math.min(indent, maxIndent)
  122. }