Skip to content

Commit

Permalink
simplify .textlintignore support
Browse files Browse the repository at this point in the history
  • Loading branch information
taichi committed Oct 25, 2021
1 parent 3518790 commit 8ee34bc
Showing 1 changed file with 9 additions and 39 deletions.
48 changes: 9 additions & 39 deletions textlint-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,12 @@ import {

import { TextlintFixRepository, AutoFix } from "./autofix";

const DEFAULT_IGNORE_PATTERNS = Object.freeze([
"**/.git/**",
"**/node_modules/**",
]);

let connection = createConnection(ProposedFeatures.all);
let documents = new TextDocuments(TextDocument);
let workspaceRoot: string;
let trace: number;
let textlintModule;
let settings;
let ignorePatterns: string[];
documents.listen(connection);
let fixrepos: Map<string /* uri */, TextlintFixRepository> = new Map();

Expand All @@ -65,28 +59,6 @@ connection.onInitialize((params) => {
});
});

function loadIgnoreFile() {
ignorePatterns = [];
ignorePatterns.push(...DEFAULT_IGNORE_PATTERNS);
const ignorePath = settings.ignorePath
? settings.ignorePath
: path.resolve(workspaceRoot, ".textlintignore");
const baseDir = path.dirname(ignorePath);
if (fs.existsSync(ignorePath)) {
const patterns = fs
.readFileSync(ignorePath, "utf-8")
.split(/\r?\n/)
.filter((line: string) => !/^\s*$/.test(line) && !/^\s*#/.test(line))
.map((pattern) => {
if (pattern.startsWith("!")) {
return "!" + path.posix.join(baseDir, pattern.slice(1));
}
return path.posix.join(baseDir, pattern);
});
ignorePatterns.push(...patterns);
}
}

function rewind() {
return resolveTextlint().then(() => {
let docs = [...fixrepos.keys()]
Expand All @@ -108,12 +80,10 @@ connection.onDidChangeConfiguration((change) => {
TRACE(`onDidChangeConfiguration ${JSON.stringify(newone)}`);
settings = newone;
trace = Trace.fromString(newone.trace);
loadIgnoreFile();
return rewind();
});
connection.onDidChangeWatchedFiles((params) => {
TRACE("onDidChangeWatchedFiles");
loadIgnoreFile();
return rewind();
});

Expand Down Expand Up @@ -222,6 +192,14 @@ function candidates(root: string) {
return () => glob.sync(`${root}/.textlintr{c.js,c.yaml,c.yml,c,c.json}`);
}

function findIgnore() {
const ignorePath =
settings.ignorePath || path.resolve(workspaceRoot, ".textlintignore");
if (fs.existsSync(ignorePath)) {
return ignorePath;
}
}

function findConfig(): string {
let roots = [
candidates(workspaceRoot),
Expand All @@ -241,14 +219,6 @@ function findConfig(): string {
}

function isTarget(file: string): boolean {
if (!ignorePatterns) {
loadIgnoreFile();
}
for (const pattern of ignorePatterns) {
if (minimatch(file, pattern)) {
return false;
}
}
const relativePath = path.relative(workspaceRoot, file);
return (
settings.targetPath === "" ||
Expand All @@ -275,7 +245,7 @@ function validate(doc: TextDocument): Thenable<void> {
if (conf && repo) {
try {
TRACE(`configuration file is ${conf}`);
return repo.newEngine(conf).then((engine) => {
return repo.newEngine(conf, findIgnore()).then((engine) => {
let ext = path.extname(URI.parse(uri).fsPath);
TRACE(`engine started... ${ext}`);
if (-1 < engine.availableExtensions.findIndex((s) => s === ext)) {
Expand Down

0 comments on commit 8ee34bc

Please sign in to comment.