|
| 1 | +import { HighlighedNode, Position } from './code'; |
| 2 | +import { DiffModel } from './diff'; |
| 3 | +import { ParsedDiffFile } from './diffParser'; |
| 4 | + |
| 5 | +export class DiffCode { |
| 6 | + public readonly path: string; |
| 7 | + public readonly filename: string; |
| 8 | + public readonly language: string; |
| 9 | + public readonly lines: string[]; |
| 10 | + public readonly displayLineNumbers: (number | undefined)[]; |
| 11 | + public readonly diffs: DiffModel; |
| 12 | + public readonly added: number; |
| 13 | + public readonly removed: number; |
| 14 | + public readonly status: 'modified' | 'added' | 'deleted' | 'renamed'; |
| 15 | + public readonly oldPath?: string; |
| 16 | + |
| 17 | + private lineOffsets: number[] = []; |
| 18 | + private totalLength: number = 0; |
| 19 | + private contentCache: string | null = null; |
| 20 | + |
| 21 | + constructor( |
| 22 | + path: string, |
| 23 | + lines: string[], |
| 24 | + displayLineNumbers: (number | undefined)[] = [], |
| 25 | + diffs: DiffModel = DiffModel.empty(), |
| 26 | + added: number = 0, |
| 27 | + removed: number = 0, |
| 28 | + status: 'modified' | 'added' | 'deleted' | 'renamed' = 'modified', |
| 29 | + oldPath?: string |
| 30 | + ) { |
| 31 | + this.path = path; |
| 32 | + this.filename = path.split('/').pop() || path; |
| 33 | + this.language = this.detectLanguage(path); |
| 34 | + this.lines = lines; |
| 35 | + this.displayLineNumbers = displayLineNumbers; |
| 36 | + this.diffs = diffs; |
| 37 | + this.added = added; |
| 38 | + this.removed = removed; |
| 39 | + this.status = status; |
| 40 | + this.oldPath = oldPath; |
| 41 | + |
| 42 | + this.computeOffsets(); |
| 43 | + } |
| 44 | + |
| 45 | + public static fromParsedFile(file: ParsedDiffFile, isOriginal: boolean = false): DiffCode { |
| 46 | + const lines = isOriginal ? file.oldLines : file.newLines; |
| 47 | + const lineNumbers = isOriginal ? file.oldLineNumbers : file.newLineNumbers; |
| 48 | + return new DiffCode( |
| 49 | + file.path, |
| 50 | + lines, |
| 51 | + lineNumbers, |
| 52 | + file.diffs, |
| 53 | + file.added, |
| 54 | + file.removed, |
| 55 | + file.status, |
| 56 | + file.oldPath |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + private detectLanguage(filePath: string): string { |
| 61 | + const ext = filePath.split('.').pop()?.toLowerCase(); |
| 62 | + switch (ext) { |
| 63 | + case 'ts': |
| 64 | + case 'tsx': |
| 65 | + return 'typescript'; |
| 66 | + case 'js': |
| 67 | + case 'jsx': |
| 68 | + return 'javascript'; |
| 69 | + case 'rs': |
| 70 | + return 'rust'; |
| 71 | + case 'py': |
| 72 | + return 'python'; |
| 73 | + case 'json': |
| 74 | + return 'json'; |
| 75 | + case 'css': |
| 76 | + return 'css'; |
| 77 | + case 'html': |
| 78 | + return 'html'; |
| 79 | + case 'md': |
| 80 | + return 'markdown'; |
| 81 | + default: |
| 82 | + return 'plaintext'; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private computeOffsets(): void { |
| 87 | + this.lineOffsets = []; |
| 88 | + let currentOffset = 0; |
| 89 | + for (let i = 0; i < this.lines.length; i++) { |
| 90 | + this.lineOffsets.push(currentOffset); |
| 91 | + currentOffset += this.lines[i].length + 1; // +1 for newline |
| 92 | + } |
| 93 | + this.totalLength = Math.max(0, currentOffset > 0 ? currentOffset - 1 : 0); |
| 94 | + } |
| 95 | + |
| 96 | + public getContent(): string { |
| 97 | + if (this.contentCache === null) { |
| 98 | + this.contentCache = this.lines.join('\n'); |
| 99 | + } |
| 100 | + return this.contentCache; |
| 101 | + } |
| 102 | + |
| 103 | + public getContentLength(): number { |
| 104 | + return this.totalLength; |
| 105 | + } |
| 106 | + |
| 107 | + public getLines(): string[] { |
| 108 | + return this.lines; |
| 109 | + } |
| 110 | + |
| 111 | + public line(index: number): string { |
| 112 | + return this.lines[index] ?? ''; |
| 113 | + } |
| 114 | + |
| 115 | + public linesLength(): number { |
| 116 | + return this.lines.length; |
| 117 | + } |
| 118 | + |
| 119 | + public getPosition(offset: number): Position { |
| 120 | + if (this.lines.length === 0) return { line: 0, column: 0 }; |
| 121 | + if (offset <= 0) return { line: 0, column: 0 }; |
| 122 | + |
| 123 | + let low = 0; |
| 124 | + let high = this.lineOffsets.length - 1; |
| 125 | + let line = 0; |
| 126 | + |
| 127 | + while (low <= high) { |
| 128 | + const mid = (low + high) >> 1; |
| 129 | + if (this.lineOffsets[mid] <= offset) { |
| 130 | + line = mid; |
| 131 | + low = mid + 1; |
| 132 | + } else { |
| 133 | + high = mid - 1; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + const lineStart = this.lineOffsets[line] ?? 0; |
| 138 | + const column = Math.max(0, offset - lineStart); |
| 139 | + return { line, column }; |
| 140 | + } |
| 141 | + |
| 142 | + public getOffset(line: number, column: number): number { |
| 143 | + if (line < 0) return 0; |
| 144 | + if (line >= this.lines.length) return this.totalLength; |
| 145 | + const lineStart = this.lineOffsets[line] ?? 0; |
| 146 | + const lineLen = this.lines[line]?.length ?? 0; |
| 147 | + return lineStart + Math.min(Math.max(0, column), lineLen); |
| 148 | + } |
| 149 | + |
| 150 | + public getIntervalContent2(start: number, end: number): string { |
| 151 | + const content = this.getContent(); |
| 152 | + return content.substring(Math.max(0, start), Math.min(content.length, end)); |
| 153 | + } |
| 154 | + |
| 155 | + public getLineNodes(_index: number): HighlighedNode[] { |
| 156 | + return []; |
| 157 | + } |
| 158 | + |
| 159 | + public getLineBinaryTokens(_index: number): Uint32Array | undefined { |
| 160 | + return undefined; |
| 161 | + } |
| 162 | + |
| 163 | + public getDiffs(): DiffModel { |
| 164 | + return this.diffs; |
| 165 | + } |
| 166 | +} |
0 commit comments