|
| 1 | +import sortedIndexBy from "lodash/sortedIndexBy" |
| 2 | +import sortedLastIndexBy from "lodash/sortedLastIndexBy" |
| 3 | +import type { LocationRange, Token, VDocumentFragment } from "../ast" |
| 4 | +import type { LinesAndColumns } from "./lines-and-columns" |
| 5 | + |
| 6 | +interface HasRange { |
| 7 | + range: [number, number] |
| 8 | +} |
| 9 | +/** |
| 10 | + * Replace the tokens in the given range. |
| 11 | + * @param document The document that the node is belonging to. |
| 12 | + * @param node The node to specify the range of replacement. |
| 13 | + * @param newTokens The new tokens. |
| 14 | + */ |
| 15 | +export function replaceTokens( |
| 16 | + document: VDocumentFragment | null, |
| 17 | + node: HasRange, |
| 18 | + newTokens: Token[], |
| 19 | +): void { |
| 20 | + if (document == null) { |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + const index = sortedIndexBy(document.tokens, node, byRange0) |
| 25 | + const count = sortedLastIndexBy(document.tokens, node, byRange1) - index |
| 26 | + document.tokens.splice(index, count, ...newTokens) |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Replace and split the tokens in the given range. |
| 31 | + * @param document The document that the node is belonging to. |
| 32 | + * @param node The node to specify the range of replacement. |
| 33 | + * @param newTokens The new tokens. |
| 34 | + */ |
| 35 | +export function replaceAndSplitTokens( |
| 36 | + document: VDocumentFragment | null, |
| 37 | + node: HasRange & { |
| 38 | + loc: LocationRange |
| 39 | + }, |
| 40 | + newTokens: Token[], |
| 41 | +): void { |
| 42 | + if (document == null) { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + const index = sortedIndexBy(document.tokens, node, byRange0) |
| 47 | + if ( |
| 48 | + document.tokens.length === index || |
| 49 | + node.range[0] < document.tokens[index].range[0] |
| 50 | + ) { |
| 51 | + // split |
| 52 | + const beforeToken = document.tokens[index - 1] |
| 53 | + const value = beforeToken.value |
| 54 | + const splitOffset = node.range[0] - beforeToken.range[0] |
| 55 | + const afterToken: Token = { |
| 56 | + type: beforeToken.type, |
| 57 | + range: [node.range[0], beforeToken.range[1]], |
| 58 | + loc: { |
| 59 | + start: { ...node.loc.start }, |
| 60 | + end: { ...beforeToken.loc.end }, |
| 61 | + }, |
| 62 | + value: value.slice(splitOffset), |
| 63 | + } |
| 64 | + beforeToken.range[1] = node.range[0] |
| 65 | + beforeToken.loc.end = { ...node.loc.start } |
| 66 | + beforeToken.value = value.slice(0, splitOffset) |
| 67 | + document.tokens.splice(index, 0, afterToken) |
| 68 | + } |
| 69 | + let lastIndex = sortedLastIndexBy(document.tokens, node, byRange1) |
| 70 | + if ( |
| 71 | + lastIndex === 0 || |
| 72 | + node.range[1] < document.tokens[lastIndex].range[1] |
| 73 | + ) { |
| 74 | + // split |
| 75 | + const beforeToken = document.tokens[lastIndex] |
| 76 | + const value = beforeToken.value |
| 77 | + const splitOffset = |
| 78 | + beforeToken.range[1] - |
| 79 | + beforeToken.range[0] - |
| 80 | + (beforeToken.range[1] - node.range[1]) |
| 81 | + const afterToken: Token = { |
| 82 | + type: beforeToken.type, |
| 83 | + range: [node.range[1], beforeToken.range[1]], |
| 84 | + loc: { |
| 85 | + start: { ...node.loc.end }, |
| 86 | + end: { ...beforeToken.loc.end }, |
| 87 | + }, |
| 88 | + value: value.slice(splitOffset), |
| 89 | + } |
| 90 | + beforeToken.range[1] = node.range[1] |
| 91 | + beforeToken.loc.end = { ...node.loc.end } |
| 92 | + beforeToken.value = value.slice(0, splitOffset) |
| 93 | + document.tokens.splice(lastIndex + 1, 0, afterToken) |
| 94 | + lastIndex++ |
| 95 | + } |
| 96 | + const count = lastIndex - index |
| 97 | + document.tokens.splice(index, count, ...newTokens) |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Insert the given comment tokens. |
| 102 | + * @param document The document that the node is belonging to. |
| 103 | + * @param newComments The comments to insert. |
| 104 | + */ |
| 105 | +export function insertComments( |
| 106 | + document: VDocumentFragment | null, |
| 107 | + newComments: Token[], |
| 108 | +): void { |
| 109 | + if (document == null || newComments.length === 0) { |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + const index = sortedIndexBy(document.comments, newComments[0], byRange0) |
| 114 | + document.comments.splice(index, 0, ...newComments) |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Create a simple token. |
| 119 | + * @param type The type of new token. |
| 120 | + * @param start The offset of the start position of new token. |
| 121 | + * @param end The offset of the end position of new token. |
| 122 | + * @param value The value of new token. |
| 123 | + * @returns The new token. |
| 124 | + */ |
| 125 | +export function createSimpleToken( |
| 126 | + type: string, |
| 127 | + start: number, |
| 128 | + end: number, |
| 129 | + value: string, |
| 130 | + linesAndColumns: LinesAndColumns, |
| 131 | +): Token { |
| 132 | + return { |
| 133 | + type, |
| 134 | + range: [start, end], |
| 135 | + loc: { |
| 136 | + start: linesAndColumns.getLocFromIndex(start), |
| 137 | + end: linesAndColumns.getLocFromIndex(end), |
| 138 | + }, |
| 139 | + value, |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Get `x.range[0]`. |
| 145 | + * @param x The object to get. |
| 146 | + * @returns `x.range[0]`. |
| 147 | + */ |
| 148 | +function byRange0(x: HasRange): number { |
| 149 | + return x.range[0] |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Get `x.range[1]`. |
| 154 | + * @param x The object to get. |
| 155 | + * @returns `x.range[1]`. |
| 156 | + */ |
| 157 | +function byRange1(x: HasRange): number { |
| 158 | + return x.range[1] |
| 159 | +} |
0 commit comments