Skip to content

Commit

Permalink
feat(settings): add ignore and *
Browse files Browse the repository at this point in the history
- `ignore` for ignore files same as `.gitignore`
- `*` is for all fileTypes
  • Loading branch information
iamcco committed Jul 24, 2021
1 parent 1b5dac4 commit fa26d7f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 9 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -120,6 +123,7 @@ languageserver config:
```jsonc
{
"sh": "linterName", // filetype: linterName or linterName[]
"*": "linterName" // `*` is for all filetypes
}
```

Expand All @@ -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
}
```

Expand All @@ -147,6 +154,7 @@ languageserver config:
```jsonc
{
"dart": "dartfmt", // filetype: formatterName or formatterName[]
"*": "linterName" // `*` is for all filetypes
}
```

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "diagnostic-languageserver",
"version": "1.11.0",
"version": "1.12.0",
"description": "diagnostic language server",
"main": "./lib/index.js",
"repository": "[email protected]:iamcco/diagnostic-languageserver.git",
Expand All @@ -27,6 +27,7 @@
"devDependencies": {
"@types/lodash": "^4.14.150",
"@types/node": "^11.11.3",
"ignore": "^5.1.8",
"typescript": "^3.8.3"
}
}
2 changes: 2 additions & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ILinterConfig {
args?: Array<string|number>
sourceName: string
formatLines?: number
ignore?: string[]
formatPattern: [string, {
sourceName?: string
sourceNameFilter?: boolean
Expand Down Expand Up @@ -66,6 +67,7 @@ export interface IFormatterConfig {
doesWriteToFile?: boolean
requiredFiles?: string[]
ignoreExitCode?: boolean | number[]
ignore?: string[]
}

// initializationOptions config
Expand Down
14 changes: 13 additions & 1 deletion src/handles/handleDiagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -154,6 +155,7 @@ async function handleLinter (
sourceName,
isStdout,
isStderr,
ignore,
securities = {}
} = config
const diagnostics: Diagnostic[] = [];
Expand All @@ -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) {
Expand Down
17 changes: 16 additions & 1 deletion src/handles/handleFormat.ts
Original file line number Diff line number Diff line change
@@ -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<string>

Expand All @@ -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)) {
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ connection.onInitialize((param: InitializeParams) => {
const handleDiagnostic = ( change: TextDocumentChangeEvent<TextDocument> ) => {
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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit fa26d7f

Please sign in to comment.