89 lines
3.0 KiB
TypeScript
89 lines
3.0 KiB
TypeScript
import * as vscode from 'vscode';
|
|
import { LinterContext } from './LinterContext';
|
|
import { SyntaxRule } from './rules/SyntaxRule';
|
|
import { BlockRule } from './rules/BlockRule';
|
|
import { MetadataRule } from './rules/MetadataRule';
|
|
import { IndentationRule } from './rules/IndentationRule';
|
|
import { ParameterRule } from './rules/ParameterRule';
|
|
|
|
export function validateTplDocument(document: vscode.TextDocument, collection: vscode.DiagnosticCollection) {
|
|
if (document.languageId !== 'tpl') {return;}
|
|
|
|
const text = document.getText();
|
|
const lines = text.split('\n');
|
|
const ctx = new LinterContext();
|
|
|
|
const rules = [
|
|
new IndentationRule(),
|
|
new SyntaxRule(),
|
|
new MetadataRule(),
|
|
new BlockRule(),
|
|
new ParameterRule()
|
|
];
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const rawLine = lines[i];
|
|
const line = rawLine.trim();
|
|
|
|
ctx.resetLineStates();
|
|
|
|
// NEU: TODO-Check hier im Linter, BEVOR Kommentare ignoriert werden!
|
|
const todoMatch = rawLine.match(/\bTODO\b\s*:?\s*(.*)/i);
|
|
if (todoMatch) {
|
|
ctx.addDiag(i, todoMatch.index!, rawLine.length, `TODO: ${todoMatch[1].trim()}`, vscode.DiagnosticSeverity.Information);
|
|
}
|
|
|
|
trackStrings(rawLine, line, ctx);
|
|
|
|
if (ctx.isInternalStringLine && (ctx.inTripleDouble || ctx.inTripleSingle)) { continue; }
|
|
|
|
if (line.startsWith('//') || line.startsWith('/*')) {
|
|
if (line.startsWith('/*')) { ctx.inMultiLineComment = true; }
|
|
continue;
|
|
}
|
|
if (ctx.inMultiLineComment) {
|
|
if (line.includes('*/')) { ctx.inMultiLineComment = false; }
|
|
continue;
|
|
}
|
|
if (line.length === 0) { continue; }
|
|
|
|
// Regeln anwenden
|
|
for (const rule of rules) {
|
|
rule.checkLine(line, rawLine, i, ctx, lines);
|
|
}
|
|
}
|
|
|
|
// Sicherer Aufruf für alle Regeln
|
|
for (const rule of rules) {
|
|
if (rule.checkEnd) {
|
|
rule.checkEnd(lines, ctx);
|
|
}
|
|
}
|
|
|
|
collection.set(document.uri, ctx.diagnostics);
|
|
}
|
|
|
|
// Interne Hilfsfunktion für den Parser
|
|
function trackStrings(rawLine: string, line: string, ctx: LinterContext) {
|
|
const tripleDoubleCount = (rawLine.match(/"""/g) || []).length;
|
|
const tripleSingleCount = (rawLine.match(/'''/g) || []).length;
|
|
|
|
if (ctx.inTripleDouble) {
|
|
ctx.isInternalStringLine = true;
|
|
if (tripleDoubleCount % 2 !== 0) { ctx.inTripleDouble = false; ctx.isClosingString = true; }
|
|
} else if (tripleDoubleCount % 2 !== 0) {
|
|
ctx.inTripleDouble = true; ctx.justOpenedString = true;
|
|
}
|
|
|
|
if (ctx.inTripleSingle) {
|
|
ctx.isInternalStringLine = true;
|
|
if (tripleSingleCount % 2 !== 0) { ctx.inTripleSingle = false; ctx.isClosingString = true; }
|
|
} else if (tripleSingleCount % 2 !== 0 && !ctx.justOpenedString) {
|
|
ctx.inTripleSingle = true; ctx.justOpenedString = true;
|
|
}
|
|
|
|
if (ctx.justOpenedString) {
|
|
ctx.stringNeedsSemicolon = !line.startsWith('"""') && !line.startsWith("'''");
|
|
}
|
|
}
|