| 19 | }; |
| 20 | |
| 21 | function getTextBlock(text: string, startPosition: number, endPosition: number) { |
| 22 | if (startPosition > endPosition || startPosition < 0 || endPosition > text.length) { |
| 23 | throw new Error("Invalid positions provided"); |
| 24 | } |
| 25 | |
| 26 | let startLineNumber = 1; |
| 27 | let startColumn = 1; |
| 28 | let endLineNumber = 1; |
| 29 | let endColumn = 1; |
| 30 | |
| 31 | for (let i = 0, currentLine = 1, currentColumn = 1; i < text.length; i += 1) { |
| 32 | if (i === startPosition) { |
| 33 | startLineNumber = currentLine; |
| 34 | startColumn = currentColumn; |
| 35 | } |
| 36 | |
| 37 | if (i === endPosition) { |
| 38 | endLineNumber = currentLine; |
| 39 | endColumn = currentColumn; |
| 40 | break; |
| 41 | } |
| 42 | |
| 43 | if (text[i] === "\n") { |
| 44 | currentLine += 1; |
| 45 | currentColumn = 0; |
| 46 | } |
| 47 | |
| 48 | currentColumn += 1; |
| 49 | } |
| 50 | |
| 51 | return { |
| 52 | startLineNumber, |
| 53 | startColumn, |
| 54 | endLineNumber, |
| 55 | endColumn, |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | self.addEventListener("message", (event) => { |
| 60 | const { code, id, config } = event.data; |