|
| 1 | +import type { VSDocument, VSElement } from './model' |
| 2 | + |
| 3 | +/** |
| 4 | + * Umbenennen (Refactoring): eine Variable an allen Stellen umbenennen, im Java-Code und im Struktogramm. |
| 5 | + * Genau dieselbe Schreibweise (`zahl` ≠ `Zahl`), nur ganze Wörter; Text in "…"/'…' und Kommentare bleiben. |
| 6 | + */ |
| 7 | + |
| 8 | +export const javaKeywords = new Set( |
| 9 | + ( |
| 10 | + 'abstract assert boolean break byte case catch char class const continue default do double else enum extends final ' + |
| 11 | + 'finally float for goto if implements import instanceof int interface long native new package private protected public ' + |
| 12 | + 'return short static strictfp super switch synchronized this throw throws transient try void volatile while ' + |
| 13 | + 'true false null var record yield' |
| 14 | + ).split(' '), |
| 15 | +) |
| 16 | + |
| 17 | +const wordChar = /[\p{L}\p{N}_$]/u |
| 18 | +const wordStart = /[\p{L}_$]/u |
| 19 | + |
| 20 | +export interface Word { |
| 21 | + from: number |
| 22 | + to: number |
| 23 | + name: string |
| 24 | +} |
| 25 | + |
| 26 | +/** Alle Wörter im Code außerhalb von Zeichenketten, Zeichen und Kommentaren. */ |
| 27 | +export function codeWords(text: string): Word[] { |
| 28 | + const words: Word[] = [] |
| 29 | + let i = 0 |
| 30 | + while (i < text.length) { |
| 31 | + const c = text[i] |
| 32 | + if (c === '/' && text[i + 1] === '/') { |
| 33 | + while (i < text.length && text[i] !== '\n') i++ |
| 34 | + } else if (c === '/' && text[i + 1] === '*') { |
| 35 | + const end = text.indexOf('*/', i + 2) |
| 36 | + i = end < 0 ? text.length : end + 2 |
| 37 | + } else if (c === '"' || c === "'") { |
| 38 | + i++ |
| 39 | + while (i < text.length && text[i] !== c && text[i] !== '\n') i += text[i] === '\\' ? 2 : 1 |
| 40 | + i++ |
| 41 | + } else if (wordStart.test(c)) { |
| 42 | + const from = i |
| 43 | + while (i < text.length && wordChar.test(text[i])) i++ |
| 44 | + words.push({ from, to: i, name: text.slice(from, i) }) |
| 45 | + } else if (/\d/.test(c)) { |
| 46 | + // Zahlen wie 1e5 oder 10L gehören nicht zu einem Namen. |
| 47 | + while (i < text.length && wordChar.test(text[i])) i++ |
| 48 | + } else i++ |
| 49 | + } |
| 50 | + return words |
| 51 | +} |
| 52 | + |
| 53 | +/** Stellen, an denen die Variable `name` vorkommt (nicht hinter einem Punkt: `feld.length` ist keine Variable `length`). */ |
| 54 | +export function nameRanges(text: string, name: string): Word[] { |
| 55 | + return codeWords(text).filter((w) => w.name === name && !afterDot(text, w.from)) |
| 56 | +} |
| 57 | + |
| 58 | +function afterDot(text: string, from: number): boolean { |
| 59 | + let i = from - 1 |
| 60 | + while (i >= 0 && (text[i] === ' ' || text[i] === '\t')) i-- |
| 61 | + return text[i] === '.' |
| 62 | +} |
| 63 | + |
| 64 | +/** Wort unter dem Cursor (auch direkt dahinter), wenn es ein umbenennbarer Name ist. */ |
| 65 | +export function nameAt(text: string, pos: number): Word | undefined { |
| 66 | + const w = codeWords(text).find((w) => pos >= w.from && pos <= w.to) |
| 67 | + if (!w || javaKeywords.has(w.name) || afterDot(text, w.from)) return undefined |
| 68 | + return w |
| 69 | +} |
| 70 | + |
| 71 | +/** Text mit umbenannter Variable und Anzahl der geänderten Stellen. */ |
| 72 | +export function renameInText(text: string, from: string, to: string): { text: string; count: number } { |
| 73 | + const ranges = nameRanges(text, from) |
| 74 | + let out = '' |
| 75 | + let last = 0 |
| 76 | + for (const r of ranges) { |
| 77 | + out += text.slice(last, r.from) + to |
| 78 | + last = r.to |
| 79 | + } |
| 80 | + return { text: out + text.slice(last), count: ranges.length } |
| 81 | +} |
| 82 | + |
| 83 | +/** Alle Texte des Struktogramms (Kopfzeile, Elemente, Fallnamen). */ |
| 84 | +function documentTexts(doc: VSDocument): string[] { |
| 85 | + const texts = [doc.caption] |
| 86 | + const walk = (elements: VSElement[]) => { |
| 87 | + for (const el of elements) { |
| 88 | + texts.push(el.textLines.join('\n')) |
| 89 | + if (el.loopBody) walk(el.loopBody) |
| 90 | + for (const c of el.cases ?? []) { |
| 91 | + texts.push(c.name) |
| 92 | + walk(c.elements) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + walk(doc.elements) |
| 97 | + return texts |
| 98 | +} |
| 99 | + |
| 100 | +export function renameInDocument(doc: VSDocument, from: string, to: string): { doc: VSDocument; count: number } { |
| 101 | + let count = 0 |
| 102 | + const rename = (s: string) => { |
| 103 | + const r = renameInText(s, from, to) |
| 104 | + count += r.count |
| 105 | + return r.text |
| 106 | + } |
| 107 | + const walk = (elements: VSElement[]): VSElement[] => |
| 108 | + elements.map((el) => ({ |
| 109 | + ...el, |
| 110 | + textLines: rename(el.textLines.join('\n')).split('\n'), |
| 111 | + ...(el.loopBody ? { loopBody: walk(el.loopBody) } : {}), |
| 112 | + ...(el.cases ? { cases: el.cases.map((c) => ({ name: rename(c.name), elements: walk(c.elements) })) } : {}), |
| 113 | + })) |
| 114 | + const next = { ...doc, caption: rename(doc.caption), elements: walk(doc.elements) } |
| 115 | + return { doc: next, count } |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Namen, die wie Variablen aussehen: keine Schlüsselwörter, keine Methoden `name(`, keine Klassen |
| 120 | + * (`Scanner sc`, `Math.max`) und nichts hinter einem Punkt. Reihenfolge wie im Text. |
| 121 | + */ |
| 122 | +export function variableNames(texts: string[]): string[] { |
| 123 | + const names = new Set<string>() |
| 124 | + const skip = new Set<string>() |
| 125 | + const declared = new Set<string>() |
| 126 | + for (const text of texts) { |
| 127 | + const words = codeWords(text).filter((w) => !afterDot(text, w.from)) |
| 128 | + words.forEach((w, i) => { |
| 129 | + // „ø“ = leere Anweisung im Struktogramm, kein Name. |
| 130 | + if (javaKeywords.has(w.name) || w.name === 'ø') return |
| 131 | + const rest = text.slice(w.to) |
| 132 | + const next = words[i + 1] |
| 133 | + const between = next ? text.slice(w.to, next.from) : '' |
| 134 | + // `Scanner sc`, `String[] namen`: das erste Wort ist ein Typ, das zweite die Variable. |
| 135 | + const isType = next !== undefined && !javaKeywords.has(next.name) && /^(?:\s*\[\s*\])*[ \t]+$/.test(between) |
| 136 | + if (isType && /^\s*(?:[=;,):]|$)/.test(text.slice(next.to))) declared.add(next.name) |
| 137 | + const isClass = /^\p{Lu}/u.test(w.name) && /^\s*\./.test(rest) |
| 138 | + if (/^\s*\(/.test(rest) || isClass || isType) skip.add(w.name) |
| 139 | + else names.add(w.name) |
| 140 | + }) |
| 141 | + } |
| 142 | + return [...names].filter((n) => !skip.has(n) || declared.has(n)) |
| 143 | +} |
| 144 | + |
| 145 | +/** Alle Wörter im Struktogramm, auch Methoden und Klassen: Der neue Name soll keinen davon treffen. */ |
| 146 | +export function documentWords(doc: VSDocument): string[] { |
| 147 | + return documentTexts(doc).flatMap((t) => codeWords(t).map((w) => w.name)) |
| 148 | +} |
| 149 | + |
| 150 | +export function countInDocument(doc: VSDocument, name: string): number { |
| 151 | + return documentTexts(doc).reduce((n, t) => n + nameRanges(t, name).length, 0) |
| 152 | +} |
| 153 | + |
| 154 | +export function documentVariableNames(doc: VSDocument): string[] { |
| 155 | + return variableNames(documentTexts(doc)) |
| 156 | +} |
| 157 | + |
| 158 | +export function elementVariableNames(el: VSElement): string[] { |
| 159 | + return variableNames([el.textLines.join('\n')]) |
| 160 | +} |
| 161 | + |
| 162 | +export type RenameCheck = { ok: true; warning?: 'exists' } | { ok: false; reason: 'empty' | 'same' | 'invalid' | 'keyword' } |
| 163 | + |
| 164 | +/** Neuen Namen prüfen; `existing` = Namen, die schon vorkommen. */ |
| 165 | +export function checkNewName(from: string, to: string, existing: Iterable<string>): RenameCheck { |
| 166 | + if (to === '') return { ok: false, reason: 'empty' } |
| 167 | + if (to === from) return { ok: false, reason: 'same' } |
| 168 | + if (!/^[\p{L}_$][\p{L}\p{N}_$]*$/u.test(to)) return { ok: false, reason: 'invalid' } |
| 169 | + if (javaKeywords.has(to)) return { ok: false, reason: 'keyword' } |
| 170 | + for (const n of existing) if (n === to) return { ok: true, warning: 'exists' } |
| 171 | + return { ok: true } |
| 172 | +} |
0 commit comments