Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Add support to exclude files (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
znck authored May 14, 2022
1 parent ffbe096 commit ce4c6cb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 34 deletions.
8 changes: 8 additions & 0 deletions .changeset/breezy-oranges-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'grammarly': minor
---

Add **Files > Include** and **Files > Exclude** setting

- Deprecate setting `grammarly.patterns` in favor of `grammarly.files.include`
- Add `grammarly.files.include` and `grammarly.files.exclude` settings for selecting documents
34 changes: 6 additions & 28 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
{
"files.exclude": {
"out": false
},
"search.exclude": {
"out": true
},
"typescript.tsc.autoDetect": "off",
"editor.formatOnSave": true,
"cSpell.words": [
"AUTOCORRECT",
"capi",
"docid",
"Emogenie",
"errored",
"freews",
"gnar",
"Grammarly",
"heatmap",
"inversify",
"languageclient",
"minicard",
"reqid",
"subalerts",
"textdocument"
],
"typescript.preferences.quoteStyle": "single",
"typescript.preferences.importModuleSpecifierEnding": "minimal",
"cSpell.enabled": true
"grammarly.selectors": [
{
"language": "markdown",
"pattern": "**/*.md"
}
]
}
31 changes: 29 additions & 2 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,34 @@
],
"required": true,
"scope": "window",
"order": 0
"order": 0,
"markdownDeprecationMessage": "Use [Files: Include](#grammarly.files.include#)"
},
"grammarly.files.include": {
"type": "array",
"markdownDescription": "Configure [glob patterns](https://code.visualstudio.com/docs/editor/codebasics#_advanced-search-options) for including files and folders.",
"items": {
"type": "string"
},
"default": [
"**/readme.md",
"**/README.md",
"**/*.txt"
],
"required": true,
"scope": "window",
"order": 1
},
"grammarly.files.exclude": {
"type": "array",
"markdownDescription": "Configure [glob patterns](https://code.visualstudio.com/docs/editor/codebasics#_advanced-search-options) for excluding files and folders.",
"items": {
"type": "string"
},
"default": [],
"required": true,
"scope": "window",
"order": 2
},
"grammarly.selectors": {
"type": "array",
Expand All @@ -75,7 +102,7 @@
"default": [],
"required": true,
"scope": "window",
"order": 1
"order": 99
},
"grammarly.startTextCheckInPausedState": {
"type": "boolean",
Expand Down
44 changes: 40 additions & 4 deletions extension/src/GrammarlyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ export class GrammarlyClient implements Registerable {
}

public matchesDocumentSelector(document: TextDocument): boolean {
return languages.match(this.selectors, document) > 0
const selector: DocumentFilter[] = workspace
.getConfiguration('grammarly')
.get<string[]>('files.exclude', [])
.map((pattern) => ({ pattern }))

return languages.match(this.selectors, document) > 0 && languages.match(selector, document) <= 0
}

private createClient(): GrammarlyLanguageClient {
Expand All @@ -44,6 +49,9 @@ export class GrammarlyClient implements Registerable {
pattern: folder != null ? new RelativePattern(folder, pattern) : pattern,
})
})
config.get<string[]>('files.include', []).forEach((pattern) => {
this.selectors.push({ pattern })
})
config.get<DocumentFilter[]>('selectors', []).forEach((selector) => {
if (folder != null && selector.pattern != null) {
this.selectors.push({
Expand Down Expand Up @@ -84,6 +92,19 @@ export class GrammarlyClient implements Registerable {
},
markdown: {
isTrusted: true,
// @ts-ignore
supportHtml: true,
},
middleware: {
didOpen: (document, next) => {
if (this.matchesDocumentSelector(document)) next(document)
},
didChange: (event, next) => {
if (this.matchesDocumentSelector(event.document)) next(event)
},
didSave: (document, next) => {
if (this.matchesDocumentSelector(document)) next(document)
},
},
},
)
Expand All @@ -93,6 +114,15 @@ export class GrammarlyClient implements Registerable {

register() {
return Disposable.from(
workspace.onDidChangeConfiguration(async (event) => {
if (
event.affectsConfiguration('grammarly.patterns') ||
event.affectsConfiguration('grammarly.files') ||
event.affectsConfiguration('grammarly.selectors')
) {
await this.start()
}
}),
window.registerUriHandler({
handleUri: async (uri) => {
if (uri.path === '/auth/callback') {
Expand All @@ -115,10 +145,16 @@ export class GrammarlyClient implements Registerable {
const document = window.activeTextEditor?.document
if (document == null) return console.log('No active document')
const status = await this.client.protocol.getDocumentStatus(document.uri.toString())
const excluded: DocumentFilter[] = workspace
.getConfiguration('grammarly')
.get<string[]>('files.exclude', [])
.map((pattern) => ({ pattern }))
if (this.matchesDocumentSelector(document) && status != null) {
await window.showInformationMessage(`Grammarly is already enabled for this file.`, {
detail: document.uri.toString(),
})
await window.showInformationMessage(`Grammarly is already enabled for this file.`)
} else if (languages.match(excluded, document) > 0) {
await window.showInformationMessage(
`This file is explicitly excluded using Grammarly > Files > Exclude setting.`,
)
} else {
const action = await window.showInformationMessage(
`Grammarly is not enabled for this file. Enable now?`,
Expand Down

0 comments on commit ce4c6cb

Please sign in to comment.