Skip to content

Commit

Permalink
Merge pull request #1096 from pjkaufman/master
Browse files Browse the repository at this point in the history
Feat: Add Ability to Ignore Files Via Regex
  • Loading branch information
pjkaufman authored Jun 1, 2024
2 parents d2dd132 + 9f238d2 commit 3e6428d
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ export default {
'add-input-button-text': 'Add another folder to ignore',
'delete-tooltip': 'Delete',
},
'files-to-ignore': {
'name': 'Files to ignore',
'description': 'Files to ignore when linting all files or linting on save.',
'file-search-placeholder-text': 'regex for file to ignore',
'add-input-button-text': 'Add another file to ignore regex',
'delete-tooltip': 'Delete',
'label-placeholder-text': 'label',
'flags-placeholder-text': 'flags',
'warning': 'Use this with caution if you do not know regex. Also, please make sure that if you use lookbehinds in your regex on iOS mobile that you are on a version that supports using them.',
},
'override-locale': {
'name': 'Override locale',
'description': 'Set this if you want to use a locale different from the default',
Expand Down Expand Up @@ -211,7 +221,7 @@ export default {
// custom-replace-option.ts
'name': 'Custom Regex Replacement',
'description': 'Custom regex replacement can be used to replace anything that matches the find regex with the replacement value. The replace and find values will need to be valid regex values.',
'warning': 'Use this with caution if you do not know regex. Also, please make sure that you do not use lookbehinds in your regex on iOS mobile as that will cause linting to fail since that is not supported on that platform.',
'warning': 'Use this with caution if you do not know regex. Also, please make sure that if you use lookbehinds in your regex on iOS mobile that you are on a version that supports using them.',
'add-input-button-text': 'Add new regex replacement',
'regex-to-find-placeholder-text': 'regex to find',
'flags-placeholder-text': 'flags',
Expand Down
12 changes: 12 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,18 @@ export default class LinterPlugin extends Plugin {
return true;
}
}

for (const fileToIgnore of this.settings.filesToIgnore) {
if (!fileToIgnore.match) {
continue;
}

const fileNameRegex = new RegExp(`${fileToIgnore.match}`, fileToIgnore.flags);
if (fileNameRegex.test(file.path)) {
return true;
}
}

return false;
}

Expand Down
3 changes: 3 additions & 0 deletions src/settings-data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Options} from './rules';
import {LintCommand} from './ui/linter-components/custom-command-option';
import {CustomReplace} from './ui/linter-components/custom-replace-option';
import {FileToIgnore} from './ui/linter-components/files-to-ignore-option';
import {NestedKeyOf} from './utils/nested-keyof';
import {NormalArrayFormats, QuoteCharacter, SpecialArrayFormats, TagSpecificArrayFormats} from './utils/yaml';

Expand All @@ -24,6 +25,7 @@ export interface LinterSettings {
lintOnFileChange: boolean,
displayLintOnFileChangeNotice: boolean,
foldersToIgnore: string[];
filesToIgnore: FileToIgnore[];
linterLocale: string;
logLevel: string;
lintCommands: LintCommand[];
Expand All @@ -42,6 +44,7 @@ export const DEFAULT_SETTINGS: Partial<LinterSettings> = {
displayLintOnFileChangeNotice: false,
settingsConvertedToConfigKeyValues: false,
foldersToIgnore: [],
filesToIgnore: [],
linterLocale: 'system-default',
logLevel: 'ERROR',
lintCommands: [],
Expand Down
21 changes: 20 additions & 1 deletion src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ textarea.full-width {
/**
* Custom regex replacement
*/
.linter-custom-regex-replacement-container div:last-child{
.linter-custom-regex-replacement-container div:last-child {
border: none;
}
.linter-custom-regex-replacement {
Expand All @@ -210,6 +210,25 @@ textarea.full-width {
width: 50%;
}

/**
* Files to ignore
*/
.linter-files-to-ignore-container div:last-child {
border: none;
}
.linter-files-to-ignore {
margin-bottom: 15px;
border: none;
border-bottom: var(--hr-thickness) solid;
border-color: var(--hr-color);
}
.linter-files-to-ignore-normal-input {
width: 40%;
}
.linter-files-to-ignore-flags {
width: 15%;
}

/**
* Setting item no border
*/
Expand Down
76 changes: 76 additions & 0 deletions src/ui/linter-components/files-to-ignore-option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {Setting, Component} from 'obsidian';
import {getTextInLanguage} from 'src/lang/helpers';
import {AddCustomRow} from '../components/add-custom-row';
export type FileToIgnore = {label: string, match: string, flags: string};

const defaultFlags = 'i';

export class FilesToIgnoreOption extends AddCustomRow {
constructor(containerEl: HTMLElement, parentComponent: Component, public filesToIgnore: FileToIgnore[], saveSettings: () => void) {
super(
containerEl,
parentComponent,
getTextInLanguage('tabs.general.files-to-ignore.name'),
getTextInLanguage('tabs.general.files-to-ignore.description'),
getTextInLanguage('tabs.general.files-to-ignore.warning'),
getTextInLanguage('tabs.general.files-to-ignore.add-input-button-text'),
saveSettings,
()=>{
const newRegex = {label: '', match: '', flags: defaultFlags};
this.filesToIgnore.push(newRegex);
this.saveSettings();
this.addFileToIgnore(newRegex, this.filesToIgnore.length - 1, true);
});
this.display();
this.inputElDiv.addClass('linter-files-to-ignore-container');
}

protected showInputEls(): void {
this.filesToIgnore.forEach((regex, index) => {
this.addFileToIgnore(regex, index);
});
}

private addFileToIgnore(regex: FileToIgnore, index: number, focusOnCommand: boolean = false) {
const newRegexDiv = this.inputElDiv.createDiv({cls: 'linter-files-to-ignore'});
new Setting(newRegexDiv).addText((cb) => {
cb.setPlaceholder(getTextInLanguage('tabs.general.files-to-ignore.label-placeholder-text'))
.setValue(regex.label)
.onChange((value) => {
this.filesToIgnore[index].label = value;
this.saveSettings();
});

cb.inputEl.addClass('linter-files-to-ignore-normal-input');
if (focusOnCommand) {
cb.inputEl.focus();
}
}).addText((cb) => {
cb.setPlaceholder(getTextInLanguage('tabs.general.files-to-ignore.file-search-placeholder-text'))
.setValue(regex.match)
.onChange((value) => {
this.filesToIgnore[index].match = value;
this.saveSettings();
});

cb.inputEl.addClass('linter-files-to-ignore-normal-input');
}).addText((cb) => {
cb.setPlaceholder(getTextInLanguage('tabs.general.files-to-ignore.flags-placeholder-text'))
.setValue(regex.flags)
.onChange((value) => {
this.filesToIgnore[index].flags = value;
this.saveSettings();
});

cb.inputEl.addClass('linter-files-to-ignore-flags');
}).addExtraButton((cb)=>{
cb.setIcon('cross')
.setTooltip(getTextInLanguage('tabs.general.files-to-ignore.delete-tooltip'))
.onClick(()=>{
this.filesToIgnore.splice(index, 1);
this.saveSettings();
this.resetInputEls();
});
});
}
}
9 changes: 9 additions & 0 deletions src/ui/linter-components/tab-components/general-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {DropdownRecordInfo, DropdownSetting} from 'src/ui/components/dropdown-se
import {NumberInputSetting} from 'src/ui/components/number-input-setting';
import {ToggleSetting} from 'src/ui/components/toggle-setting';
import {FolderIgnoreOption} from '../folder-ignore-option';
import {FilesToIgnoreOption} from '../files-to-ignore-option';

export class GeneralTab extends Tab {
constructor(navEl: HTMLElement, settingsEl: HTMLElement, isMobile: boolean, plugin: LinterPlugin, private app: App) {
Expand Down Expand Up @@ -99,6 +100,14 @@ export class GeneralTab extends Tab {
const folderIgnore = new FolderIgnoreOption(folderIgnoreEl, this.plugin.settingsTab.component, this.plugin.settings.foldersToIgnore, this.app, () => {
this.plugin.saveSettings();
});

this.addSettingSearchInfo(folderIgnoreEl, folderIgnore.name, folderIgnore.description.replaceAll('\n', ' '));

const filesToIgnoreEl = this.contentEl.createDiv();
const filesToIgnore = new FilesToIgnoreOption(filesToIgnoreEl, this.plugin.settingsTab.component, this.plugin.settings.filesToIgnore, () => {
this.plugin.saveSettings();
});

this.addSettingSearchInfo(filesToIgnoreEl, filesToIgnore.name, filesToIgnore.description.replaceAll('\n', ' '));
}
}

0 comments on commit 3e6428d

Please sign in to comment.