Skip to content

Commit

Permalink
feat(file types): Allow users to configure file types to be watched (#46
Browse files Browse the repository at this point in the history
)

* feat(file types): Allow users to configure file types to be watched

Allows users to configure the file types that they want to be watched by
the plugin.

* docs(README.md): Add documentation for custom file extensions
  • Loading branch information
blakedietz authored May 25, 2019
1 parent e7497c3 commit 7e874cf
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 17 deletions.
37 changes: 26 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ or yaml frontmatter with a tags property (square brackets style)
title: Hello nested tags
tags: [topic, here/is/a/nested/example]
---

```

or yaml frontmatter with a tags property (unordered list style)
Expand All @@ -27,13 +28,36 @@ or yaml frontmatter with a tags property (unordered list style)
---
title: Hello nested tags
tags:
- topic
- here/is/a/nested/example
- topic
- here/is/a/nested/example
---

```

will be visible from the file tab under a "Tag Tree" view.

## Extension Settings

### Configurations

| Name | Type | Description |
| ---------------------------------------- | ------------- | --------------------------------------------- |
| `vscode-nested-tags.additionalFileTypes` | Array<string> | Additional file types to introspect for tags. |

### Custom file extensions

You can define custom file extensions in your `settings.json`. These file extensions allow the plugin to look at more than just markdown files for the tag system.

Here's an example `settings.json` file.

```json
{
"vscode-nested-tags.additionalFileTypes": ["tex", "html"]
}
```

Now all `.tex` and `.html` files till be watched alongside `.md` files.

## Requirements

### Operating system
Expand All @@ -43,12 +67,3 @@ This extension has only been tested on macOS.
### Code

vs code 1.30.0 is required at a minimum.

## Extension Settings

## Known Issues

Currently this extension does not allow you to do the following:

- Have multiple workspace directories
- Click on a file that is listed in the tag tree
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,18 @@
"name": "Tag Tree"
}
]
}
},
"configuration": {
"type": "object",
"title": "nested-tags",
"properties": {
"vscode-nested-tags.additionalFileTypes": {
"type":"array",
"default": [],
"description": "Add file types that you want nested tags to scan for @nested-tags annotations."
}
}
}
},
"icon": "images/icon.png",
"galleryBanner": {
Expand Down
35 changes: 30 additions & 5 deletions src/tag-tree-data-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ class TagTreeDataProvider
* Add all files in the current workspace folder to the tag tree
*/
(async () => {
const uris = await vscode.workspace.findFiles("**/*.md");
// @ts-ignore
const additionalFileTypes: string[] = vscode.workspace.getConfiguration().get('vscode-nested-tags.additionalFileTypes');
const customGlobPattern = additionalFileTypes.length > 0
? `,${additionalFileTypes.join(',')}`
: '';
const globPattern = `{md${customGlobPattern}}`;
const uris = await vscode.workspace.findFiles(`**/*.${globPattern}`);
const infos = await Promise.all(
uris.map(uri => this.getTagsFromFileOnFileSystem(uri.fsPath))
);
Expand Down Expand Up @@ -123,7 +129,7 @@ class TagTreeDataProvider
* @param changeEvent
*/
private async onWillSaveTextDocument(changeEvent: vscode.TextDocumentWillSaveEvent): Promise<void> {
if (changeEvent.document.isDirty && changeEvent.document.languageId === "markdown") {
if (changeEvent.document.isDirty && this.matchesWatchedFileExtensions(changeEvent.document.uri)) {
const filePath = changeEvent.document.fileName;
const fileInfo = await this.getTagsFromFileOnFileSystem(filePath);
const tagsInTreeForFile = this.tagTree.getTagsForFile(filePath);
Expand All @@ -141,8 +147,8 @@ class TagTreeDataProvider
*/
private onDocumentChanged(changeEvent: vscode.TextDocumentChangeEvent): void {
const filePath = changeEvent.document.fileName;
// If the file has been saved and the file is a markdown file allow for making changes to the tag tree
if (filePath !== undefined && changeEvent.document.languageId === "markdown") {
// If the file has been saved and the file is a watched file type allow for making changes to the tag tree
if (filePath !== undefined && this.matchesWatchedFileExtensions(changeEvent.document.uri)) {
const fileInfo = this.getTagsFromFileText(changeEvent.document.getText(), filePath);
const tagsInTreeForFile = this.tagTree.getTagsForFile(filePath);
const isUpdateNeeded = !setsAreEqual(fileInfo.tags, tagsInTreeForFile);
Expand Down Expand Up @@ -209,7 +215,9 @@ class TagTreeDataProvider
const tagsToAdd = currentLine
.split('@nested-tags:')
.pop()
.split('-->')[0]
// Do some best effort cleanup on common multi-line comment closing syntax
.replace('-->', '')
.replace('*/', '')
.split(',');
return {...accumulator, tags: new Set([...accumulator.tags,...tagsToAdd])};
}
Expand Down Expand Up @@ -240,6 +248,23 @@ class TagTreeDataProvider

return relativePath;
}

/**
* Checks to see if a given file uri matches the file extensions that are user configured.
*
* @param uri
*/
private matchesWatchedFileExtensions(uri: Uri) {
const supportedFileExtensions = new Set([
'md',
// @ts-ignore
...vscode.workspace.getConfiguration().get('vscode-nested-tags.additionalFileTypes')
]);

const fileExtension = uri.fsPath.split('.').pop();

return supportedFileExtensions.has(fileExtension);
}
}

export { TagTreeDataProvider };

0 comments on commit 7e874cf

Please sign in to comment.