diff --git a/README.md b/README.md index 2d0af78..74df69d 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,9 @@ languageserver config: "offsetLine": 0, // offsetline "offsetColumn": 0, // offsetColumn "sourceName": "shellcheck", // source name + "ignore": [".git", "dist/"] // ignore pattern same as `.gitignore` + // don't forget to add `rootPatterns` when using `ignore` + // it need workspace to filter // Using regular expressions: "formatLines": 1, // how much lines for formatPattern[0] to match @@ -120,6 +123,7 @@ languageserver config: ```jsonc { "sh": "linterName", // filetype: linterName or linterName[] + "*": "linterName" // `*` is for all filetypes } ``` @@ -136,9 +140,12 @@ languageserver config: "requiredFiles": [ // only run formatter if any of these files exist. optional ".run_dartfmt", ], - "ignoreExitCode": false // ignore exit code. default false: exit code > 0 will not change the file. + "ignoreExitCode": false, // ignore exit code. default false: exit code > 0 will not change the file. // some formatter may exit with code > 0 so you need set it to true or number[] // exit code array that you want to ignore. + "ignore": [".git", "dist/"] // ignore pattern same as `.gitignore` + // don't forget to add `rootPatterns` when using `ignore` + // it need workspace to filter } ``` @@ -147,6 +154,7 @@ languageserver config: ```jsonc { "dart": "dartfmt", // filetype: formatterName or formatterName[] + "*": "linterName" // `*` is for all filetypes } ``` diff --git a/package.json b/package.json index 812479f..7e35394 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "diagnostic-languageserver", - "version": "1.11.0", + "version": "1.12.0", "description": "diagnostic language server", "main": "./lib/index.js", "repository": "git@github.com:iamcco/diagnostic-languageserver.git", @@ -27,6 +27,7 @@ "devDependencies": { "@types/lodash": "^4.14.150", "@types/node": "^11.11.3", + "ignore": "^5.1.8", "typescript": "^3.8.3" } } diff --git a/src/common/types.ts b/src/common/types.ts index a6d1cb1..783f182 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -13,6 +13,7 @@ export interface ILinterConfig { args?: Array sourceName: string formatLines?: number + ignore?: string[] formatPattern: [string, { sourceName?: string sourceNameFilter?: boolean @@ -66,6 +67,7 @@ export interface IFormatterConfig { doesWriteToFile?: boolean requiredFiles?: string[] ignoreExitCode?: boolean | number[] + ignore?: string[] } // initializationOptions config diff --git a/src/handles/handleDiagnostic.ts b/src/handles/handleDiagnostic.ts index a482b7b..ef3be31 100644 --- a/src/handles/handleDiagnostic.ts +++ b/src/handles/handleDiagnostic.ts @@ -7,7 +7,8 @@ import { import { URI } from 'vscode-uri'; import { Subscription, Subject, from, timer } from 'rxjs'; import { filter, switchMap, map } from 'rxjs/operators'; -import { isAbsolute, join } from 'path'; +import { isAbsolute, join, relative } from 'path'; +import ig from 'ignore'; import { waitMap } from '../common/observable'; import { ILinterConfig, SecurityKey, ILinterResult } from '../common/types'; @@ -154,6 +155,7 @@ async function handleLinter ( sourceName, isStdout, isStderr, + ignore, securities = {} } = config const diagnostics: Diagnostic[] = []; @@ -178,6 +180,16 @@ async function handleLinter ( const currentFile = URI.parse(textDocument.uri).fsPath const workDir = await findWorkDirectory(currentFile, rootPatterns) + // ignore file + const relPath = relative(workDir, currentFile) + try { + if (!isAbsolute(relPath) && ignore && ig().add(ignore).ignores(relPath)) { + return diagnostics + } + } catch (err) { + logger.error(`ignore error: ${err.message || err.name || err}`) + } + logger.info(`found working directory ${workDir}`) if (config.requiredFiles && config.requiredFiles.length) { diff --git a/src/handles/handleFormat.ts b/src/handles/handleFormat.ts index 23a572e..37e020e 100644 --- a/src/handles/handleFormat.ts +++ b/src/handles/handleFormat.ts @@ -1,10 +1,13 @@ import fs from 'fs' import { TextEdit, TextDocument, CancellationToken, Range, Position } from 'vscode-languageserver'; import { URI } from 'vscode-uri'; +import ig from 'ignore'; import { IFormatterConfig } from '../common/types'; import { findWorkDirectory, findCommand, executeFile, checkAnyFileExists } from '../common/util'; import HunkStream from '../common/hunkStream'; +import { relative, isAbsolute } from 'path'; +import logger from '../common/logger'; type Handle = (text: string) => Promise @@ -20,12 +23,24 @@ async function handleFormat( isStdout, isStderr, args = [], - ignoreExitCode + ignoreExitCode, + ignore } = config const workDir = await findWorkDirectory( URI.parse(textDocument.uri).fsPath, rootPatterns ) + const currentFile = URI.parse(textDocument.uri).fsPath + const relPath = relative(workDir, currentFile) + + try { + // ignore file + if (!isAbsolute(relPath) && ignore && ig().add(ignore).ignores(relPath)) { + return next(text) + } + } catch (err) { + logger.error(`ignore error: ${err.message || err.name || err}`) + } if (config.requiredFiles && config.requiredFiles.length) { if (!checkAnyFileExists(workDir, config.requiredFiles)) { diff --git a/src/index.ts b/src/index.ts index d166f56..8ae1aa4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,10 +67,10 @@ connection.onInitialize((param: InitializeParams) => { const handleDiagnostic = ( change: TextDocumentChangeEvent ) => { const textDocument = change.document const { linters = {}, filetypes = {} } = config - if (!filetypes[textDocument.languageId]) { + if (!filetypes[textDocument.languageId] && !filetypes['*']) { return } - const linter = [].concat(filetypes[textDocument.languageId]) + const linter = [].concat(filetypes[textDocument.languageId]).concat(filetypes['*']) const configItems = linter.map(l => linters[l]).filter(l => l) if (configItems.length === 0) { return @@ -105,10 +105,10 @@ connection.onDocumentFormatting(async ( return } const { formatters, formatFiletypes } = config - if (!formatFiletypes[doc.languageId]) { + if (!formatFiletypes[doc.languageId] && !formatFiletypes['*']) { return } - const formatterNames = [].concat(formatFiletypes[doc.languageId]) + const formatterNames = [].concat(formatFiletypes[doc.languageId]).concat(formatFiletypes['*']) const formatterConfigs = formatterNames.map(n => formatters[n]).filter(n => n) if (formatterConfigs.length === 0) { return diff --git a/yarn.lock b/yarn.lock index 41c33f5..5876645 100644 --- a/yarn.lock +++ b/yarn.lock @@ -181,7 +181,7 @@ graceful-fs@^4.2.4: resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb" integrity sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw== -ignore@^5.1.4: +ignore@^5.1.4, ignore@^5.1.8: version "5.1.8" resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57" integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==