40dd56f18825e60f0cf3b3ca03e02e0b509ece6b302b6c1471e49320180d7e40ebac555df418c235526249abeb1c4c7a3fb316479cb66073a5e68b70f325f8 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { getContextBefore } from "../line/highlight.js"
  2. import { Pos } from "../line/pos.js"
  3. import { getLine } from "../line/utils_line.js"
  4. import { replaceRange } from "../model/changes.js"
  5. import { Range } from "../model/selection.js"
  6. import { replaceOneSelection } from "../model/selection_updates.js"
  7. import { countColumn, Pass, spaceStr } from "../util/misc.js"
  8. // Indent the given line. The how parameter can be "smart",
  9. // "add"/null, "subtract", or "prev". When aggressive is false
  10. // (typically set to true for forced single-line indents), empty
  11. // lines are not indented, and places where the mode returns Pass
  12. // are left alone.
  13. export function indentLine(cm, n, how, aggressive) {
  14. let doc = cm.doc, state
  15. if (how == null) how = "add"
  16. if (how == "smart") {
  17. // Fall back to "prev" when the mode doesn't have an indentation
  18. // method.
  19. if (!doc.mode.indent) how = "prev"
  20. else state = getContextBefore(cm, n).state
  21. }
  22. let tabSize = cm.options.tabSize
  23. let line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize)
  24. if (line.stateAfter) line.stateAfter = null
  25. let curSpaceString = line.text.match(/^\s*/)[0], indentation
  26. if (!aggressive && !/\S/.test(line.text)) {
  27. indentation = 0
  28. how = "not"
  29. } else if (how == "smart") {
  30. indentation = doc.mode.indent(state, line.text.slice(curSpaceString.length), line.text)
  31. if (indentation == Pass || indentation > 150) {
  32. if (!aggressive) return
  33. how = "prev"
  34. }
  35. }
  36. if (how == "prev") {
  37. if (n > doc.first) indentation = countColumn(getLine(doc, n-1).text, null, tabSize)
  38. else indentation = 0
  39. } else if (how == "add") {
  40. indentation = curSpace + cm.options.indentUnit
  41. } else if (how == "subtract") {
  42. indentation = curSpace - cm.options.indentUnit
  43. } else if (typeof how == "number") {
  44. indentation = curSpace + how
  45. }
  46. indentation = Math.max(0, indentation)
  47. let indentString = "", pos = 0
  48. if (cm.options.indentWithTabs)
  49. for (let i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t"}
  50. if (pos < indentation) indentString += spaceStr(indentation - pos)
  51. if (indentString != curSpaceString) {
  52. replaceRange(doc, indentString, Pos(n, 0), Pos(n, curSpaceString.length), "+input")
  53. line.stateAfter = null
  54. return true
  55. } else {
  56. // Ensure that, if the cursor was in the whitespace at the start
  57. // of the line, it is moved to the end of that space.
  58. for (let i = 0; i < doc.sel.ranges.length; i++) {
  59. let range = doc.sel.ranges[i]
  60. if (range.head.line == n && range.head.ch < curSpaceString.length) {
  61. let pos = Pos(n, curSpaceString.length)
  62. replaceOneSelection(doc, i, new Range(pos, pos))
  63. break
  64. }
  65. }
  66. }
  67. }