Skip to content

Commit ec657ea

Browse files
authored
Merge pull request #1189 from pjkaufman/master
Hide Unreachable Settings
2 parents 64a94f5 + b253753 commit ec657ea

File tree

12 files changed

+210
-62
lines changed

12 files changed

+210
-62
lines changed

src/lang/locale/en.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,6 @@ export default {
152152
'name': 'Display Lint on File Change Message',
153153
'description': 'Displays a message when `Lint on Focused File Change` occurs',
154154
},
155-
'timestamp-update-on-file-contents-updated': {
156-
'name': 'Update YAML Timestamp on File Contents Update',
157-
'description': 'When the currently active file is modified, `YAML Timestamp` is run on the file. This should update the modified file timestamp if it is more than 5 seconds off from the current value.',
158-
},
159155
'folders-to-ignore': {
160156
'name': 'Folders to ignore',
161157
'description': 'Folders to ignore when linting all files or linting on save.',
@@ -849,6 +845,10 @@ export default {
849845
'name': 'Convert Local Time to UTC',
850846
'description': 'Uses UTC equivalent for saved dates instead of local time',
851847
},
848+
'update-on-file-contents-updated': {
849+
'name': 'Update YAML Timestamp on File Contents Update',
850+
'description': 'When the currently active note is modified, `YAML Timestamp` is run on the note. This should update the modified note timestamp if it is more than 5 seconds off from the current value.',
851+
},
852852
},
853853
// yaml-title-alias.ts
854854
'yaml-title-alias': {

src/main.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,16 @@ export default class LinterPlugin extends Plugin {
127127
setLogLevel(this.settings.logLevel);
128128
await this.setOrUpdateMomentInstance();
129129

130+
let updateMade = false;
130131
if (!this.settings.settingsConvertedToConfigKeyValues) {
131-
this.moveConfigValuesToKeyBasedFormat();
132+
updateMade = await this.moveConfigValuesToKeyBasedFormat();
133+
}
134+
135+
if ('lintOnFileContentChangeDelay' in this.settings) {
136+
this.settings.ruleConfigs['yaml-timestamp']['update-on-file-contents-updated'] = this.settings['lintOnFileContentChangeDelay'];
137+
138+
delete this.settings['lintOnFileContentChangeDelay'];
139+
updateMade = true;
132140
}
133141

134142
// make sure to load the defaults of any missing rules to make sure they do not cause issues on the settings page
@@ -142,10 +150,12 @@ export default class LinterPlugin extends Plugin {
142150
const defaults = rule.getDefaultOptions();
143151
if (!('english-symbols-punctuation-before' in this.settings.ruleConfigs[rule.alias])) {
144152
this.settings.ruleConfigs[rule.alias]['english-symbols-punctuation-before'] = defaults['english-symbols-punctuation-before'];
153+
updateMade = true;
145154
}
146155

147156
if (!('english-symbols-punctuation-after' in this.settings.ruleConfigs[rule.alias])) {
148157
this.settings.ruleConfigs[rule.alias]['english-symbols-punctuation-after'] = defaults['english-symbols-punctuation-after'];
158+
updateMade = true;
149159
}
150160
} else if (rule.alias == 'yaml-timestamp') {
151161
const defaults = rule.getDefaultOptions();
@@ -158,18 +168,23 @@ export default class LinterPlugin extends Plugin {
158168
}
159169
}
160170

161-
162171
delete this.settings.ruleConfigs[rule.alias]['force-retention-of-create-value'];
172+
updateMade = true;
163173
}
164174

165175
if (!('date-modified-source-of-truth' in this.settings.ruleConfigs[rule.alias])) {
166176
this.settings.ruleConfigs[rule.alias]['date-modified-source-of-truth'] = defaults['date-modified-source-of-truth'];
177+
updateMade = true;
167178
}
168179
}
169180
}
170181

171182
this.updatePasteOverrideStatus();
172183
this.updateHasCustomCommandStatus();
184+
185+
if (updateMade) {
186+
await this.saveSettings();
187+
}
173188
}
174189

175190
async saveSettings() {
@@ -290,7 +305,7 @@ export default class LinterPlugin extends Plugin {
290305
this.eventRefs.push(eventRef);
291306

292307
eventRef = this.app.workspace.on('editor-change', async (editor: Editor, info: MarkdownView | MarkdownFileInfo) => {
293-
if (this.settings.lintOnFileContentChangeDelay == AfterFileChangeLintTimes.Never) {
308+
if ((this.settings.ruleConfigs['yaml-timestamp']['update-on-file-contents-updated'] ?? AfterFileChangeLintTimes.Never) == AfterFileChangeLintTimes.Never) {
294309
return;
295310
}
296311

@@ -610,7 +625,7 @@ export default class LinterPlugin extends Plugin {
610625

611626
private createDebouncedFileUpdate(): Debouncer<[TFile, Editor], Promise<void>> {
612627
let delay = 5000;
613-
switch (this.settings.lintOnFileContentChangeDelay) {
628+
switch (this.settings.ruleConfigs['yaml-timestamp']['update-on-file-contents-updated'] ?? AfterFileChangeLintTimes.Never) {
614629
case AfterFileChangeLintTimes.After10Seconds:
615630
delay = 10000;
616631
break;
@@ -909,9 +924,10 @@ export default class LinterPlugin extends Plugin {
909924
return editor.getLine(selection.anchor.line);
910925
}
911926

912-
private moveConfigValuesToKeyBasedFormat() {
927+
private async moveConfigValuesToKeyBasedFormat(): Promise<boolean> {
913928
setLanguage('en');
914929

930+
let updateMade = false;
915931
for (const rule of rules) {
916932
const ruleName = getTextInLanguage('rules.' + rule.alias + '.name' as LanguageStringKey);
917933
const ruleSettings = this.settings.ruleConfigs[ruleName];
@@ -935,13 +951,17 @@ export default class LinterPlugin extends Plugin {
935951

936952
this.settings.ruleConfigs[rule.alias] = newSettingValues;
937953
delete this.settings.ruleConfigs[ruleName];
954+
955+
updateMade = true;
938956
}
939957
}
940958

941959
this.settings.settingsConvertedToConfigKeyValues = true;
942-
void this.saveSettings();
960+
await this.saveSettings();
943961

944962
setLanguage(window.localStorage.getItem('language'));
963+
964+
return updateMade;
945965
}
946966

947967
private getAllFilesInFolder(startingFolder: TFolder): TFile[] {

src/option.ts

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Setting} from 'obsidian';
22
import {getTextInLanguage, LanguageStringKey} from './lang/helpers';
33
import LinterPlugin from './main';
4-
import {parseTextToHTMLWithoutOuterParagraph} from './ui/helpers';
4+
import {hideEl, parseTextToHTMLWithoutOuterParagraph, unhideEl} from './ui/helpers';
55
import {LinterSettings} from './settings-data';
66
import {AutoCorrectFilesPickerOption} from './ui/linter-components/auto-correct-files-picker-option';
77

@@ -11,6 +11,7 @@ export type SearchOptionInfo = {name: string, description: string, options?: Dro
1111

1212
export abstract class Option {
1313
public ruleAlias: string;
14+
protected setting: Setting;
1415

1516
/**
1617
* Create an option
@@ -43,38 +44,55 @@ export abstract class Option {
4344
settings.ruleConfigs[this.ruleAlias][this.configKey] = value;
4445
}
4546

46-
protected parseNameAndDescriptionAndRemoveSettingBorder(setting: Setting, plugin: LinterPlugin) {
47-
parseTextToHTMLWithoutOuterParagraph(plugin.app, this.getName(), setting.nameEl, plugin.settingsTab.component);
48-
parseTextToHTMLWithoutOuterParagraph(plugin.app, this.getDescription(), setting.descEl, plugin.settingsTab.component);
47+
protected parseNameAndDescriptionAndRemoveSettingBorder(plugin: LinterPlugin) {
48+
parseTextToHTMLWithoutOuterParagraph(plugin.app, this.getName(), this.setting.nameEl, plugin.settingsTab.component);
49+
parseTextToHTMLWithoutOuterParagraph(plugin.app, this.getDescription(), this.setting.descEl, plugin.settingsTab.component);
4950

50-
setting.settingEl.addClass('linter-no-border');
51-
setting.descEl.addClass('linter-no-padding-top');
51+
this.setting.settingEl.addClass('linter-no-border');
52+
this.setting.descEl.addClass('linter-no-padding-top');
53+
}
54+
55+
hide() {
56+
hideEl(this.setting.settingEl);
57+
}
58+
59+
unhide() {
60+
unhideEl(this.setting.settingEl);
5261
}
5362
}
5463

5564
export class BooleanOption extends Option {
5665
public defaultValue: boolean;
5766

67+
constructor(configKey: string, nameKey: LanguageStringKey, descriptionKey: LanguageStringKey, defaultValue: any, ruleAlias?: string | null, private onChange?: (value: boolean) => void) {
68+
super(configKey, nameKey, descriptionKey, defaultValue, ruleAlias);
69+
}
70+
5871
public display(containerEl: HTMLElement, settings: LinterSettings, plugin: LinterPlugin): void {
59-
const setting = new Setting(containerEl)
72+
this.setting = new Setting(containerEl)
6073
.addToggle((toggle) => {
6174
toggle.setValue(settings.ruleConfigs[this.ruleAlias][this.configKey]);
6275
toggle.onChange((value) => {
6376
this.setOption(value, settings);
6477
plugin.settings = settings;
78+
79+
if (this.onChange) {
80+
this.onChange(value);
81+
}
82+
6583
void plugin.saveSettings();
6684
});
6785
});
6886

69-
this.parseNameAndDescriptionAndRemoveSettingBorder(setting, plugin);
87+
this.parseNameAndDescriptionAndRemoveSettingBorder(plugin);
7088
}
7189
}
7290

7391
export class TextOption extends Option {
7492
public defaultValue: string;
7593

7694
public display(containerEl: HTMLElement, settings: LinterSettings, plugin: LinterPlugin): void {
77-
const setting = new Setting(containerEl)
95+
this.setting = new Setting(containerEl)
7896
.addText((textbox) => {
7997
textbox.setValue(settings.ruleConfigs[this.ruleAlias][this.configKey]);
8098
textbox.onChange((value) => {
@@ -84,15 +102,15 @@ export class TextOption extends Option {
84102
});
85103
});
86104

87-
this.parseNameAndDescriptionAndRemoveSettingBorder(setting, plugin);
105+
this.parseNameAndDescriptionAndRemoveSettingBorder(plugin);
88106
}
89107
}
90108

91109
export class TextAreaOption extends Option {
92110
public defaultValue: string;
93111

94112
public display(containerEl: HTMLElement, settings: LinterSettings, plugin: LinterPlugin): void {
95-
const setting = new Setting(containerEl)
113+
this.setting = new Setting(containerEl)
96114
.addTextArea((textbox) => {
97115
textbox.setValue(settings.ruleConfigs[this.ruleAlias][this.configKey]);
98116
textbox.onChange((value) => {
@@ -102,15 +120,15 @@ export class TextAreaOption extends Option {
102120
});
103121
});
104122

105-
this.parseNameAndDescriptionAndRemoveSettingBorder(setting, plugin);
123+
this.parseNameAndDescriptionAndRemoveSettingBorder(plugin);
106124
}
107125
}
108126

109127
export class MomentFormatOption extends Option {
110128
public defaultValue: boolean;
111129

112130
public display(containerEl: HTMLElement, settings: LinterSettings, plugin: LinterPlugin): void {
113-
const setting = new Setting(containerEl)
131+
this.setting = new Setting(containerEl)
114132
.addMomentFormat((format) => {
115133
format.setValue(settings.ruleConfigs[this.ruleAlias][this.configKey]);
116134
format.setPlaceholder('dddd, MMMM Do YYYY, h:mm:ss a');
@@ -121,7 +139,7 @@ export class MomentFormatOption extends Option {
121139
});
122140
});
123141

124-
this.parseNameAndDescriptionAndRemoveSettingBorder(setting, plugin);
142+
this.parseNameAndDescriptionAndRemoveSettingBorder(plugin);
125143
}
126144
}
127145

@@ -153,7 +171,7 @@ export class DropdownOption extends Option {
153171
}
154172

155173
public display(containerEl: HTMLElement, settings: LinterSettings, plugin: LinterPlugin): void {
156-
const setting = new Setting(containerEl)
174+
this.setting = new Setting(containerEl)
157175
.addDropdown((dropdown) => {
158176
// First, add all the available options
159177
for (const option of this.options) {
@@ -170,21 +188,32 @@ export class DropdownOption extends Option {
170188
});
171189
});
172190

173-
this.parseNameAndDescriptionAndRemoveSettingBorder(setting, plugin);
191+
this.parseNameAndDescriptionAndRemoveSettingBorder(plugin);
174192
}
175193
}
176194

177195

178196
export class MdFilePickerOption extends Option {
197+
private settingEl: HTMLDivElement;
179198
constructor(configKey: string, nameKey: LanguageStringKey, descriptionKey: LanguageStringKey, ruleAlias?: string | null) {
180199
super(configKey, nameKey, descriptionKey, [], ruleAlias);
181200
}
182201

183202
public display(containerEl: HTMLElement, settings: LinterSettings, plugin: LinterPlugin): void {
184203
settings.ruleConfigs[this.ruleAlias][this.configKey] = settings.ruleConfigs[this.ruleAlias][this.configKey] ?? [];
185204

186-
new AutoCorrectFilesPickerOption(containerEl, plugin.settingsTab.component, settings.ruleConfigs[this.ruleAlias][this.configKey], plugin.app, () => {
205+
this.settingEl = containerEl.createDiv();
206+
207+
new AutoCorrectFilesPickerOption(this.settingEl, plugin.settingsTab.component, settings.ruleConfigs[this.ruleAlias][this.configKey], plugin.app, () => {
187208
void plugin.saveSettings();
188209
}, this.nameKey, this.descriptionKey);
189210
}
211+
212+
override hide() {
213+
hideEl(this.settingEl);
214+
}
215+
216+
override unhide() {
217+
unhideEl(this.settingEl);
218+
}
190219
}

src/rules.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,17 @@ export class Rule {
5656
) {
5757
this.ruleHeading = this.getName().toLowerCase().replaceAll(' ', '-');
5858

59-
options.unshift(new BooleanOption('enabled', this.descriptionKey, '' as LanguageStringKey, false));
59+
options.unshift(new BooleanOption('enabled', this.descriptionKey, '' as LanguageStringKey, false, alias, (value: boolean) => {
60+
if (options.length > 1) {
61+
for (let i = 1; i < options.length; i++) {
62+
if (value) {
63+
options[i].unhide();
64+
} else {
65+
options[i].hide();
66+
}
67+
}
68+
}
69+
}));
6070
for (const option of options) {
6171
option.ruleAlias = alias;
6272
}

src/rules/yaml-timestamp.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {escapeDollarSigns} from '../utils/regex';
77
import {insert} from '../utils/strings';
88
import parseFormat from 'moment-parseformat';
99
import {getTextInLanguage} from '../lang/helpers';
10+
import {AfterFileChangeLintTimes} from '../settings-data';
1011

1112
type DateCreatedSourceOfTruth = 'file system' | 'frontmatter';
1213
type DateModifiedSourceOfTruth = 'file system' | 'user or Linter edits';
@@ -29,6 +30,10 @@ class YamlTimestampOptions implements Options {
2930

3031
convertToUTC?: boolean = false;
3132

33+
// This is not used in the rule itself. It is used for running this rule as a standalone when
34+
// editor content is updated.
35+
timestampUpdateOnFileContentUpdated?: AfterFileChangeLintTimes =AfterFileChangeLintTimes.Never;
36+
3237
@RuleBuilder.noSettingControl()
3338
fileModifiedTime?: string;
3439

@@ -455,6 +460,38 @@ export default class YamlTimestamp extends RuleBuilder<YamlTimestampOptions> {
455460
descriptionKey: 'rules.yaml-timestamp.convert-to-utc.description',
456461
optionsKey: 'convertToUTC',
457462
}),
463+
new DropdownOptionBuilder<YamlTimestampOptions, AfterFileChangeLintTimes>({
464+
OptionsClass: YamlTimestampOptions,
465+
nameKey: 'rules.yaml-timestamp.update-on-file-contents-updated.name',
466+
descriptionKey: 'rules.yaml-timestamp.update-on-file-contents-updated.description',
467+
optionsKey: 'timestampUpdateOnFileContentUpdated',
468+
records: [
469+
{
470+
value: AfterFileChangeLintTimes.Never,
471+
description: AfterFileChangeLintTimes.Never,
472+
},
473+
{
474+
value: AfterFileChangeLintTimes.After5Seconds,
475+
description: AfterFileChangeLintTimes.After5Seconds,
476+
},
477+
{
478+
value: AfterFileChangeLintTimes.After10Seconds,
479+
description: AfterFileChangeLintTimes.After10Seconds,
480+
},
481+
{
482+
value: AfterFileChangeLintTimes.After15Seconds,
483+
description: AfterFileChangeLintTimes.After15Seconds,
484+
},
485+
{
486+
value: AfterFileChangeLintTimes.After30Seconds,
487+
description: AfterFileChangeLintTimes.After30Seconds,
488+
},
489+
{
490+
value: AfterFileChangeLintTimes.After1Minute,
491+
description: AfterFileChangeLintTimes.After1Minute,
492+
},
493+
],
494+
}),
458495
];
459496
}
460497
}

src/settings-data.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export interface LinterSettings {
3333
recordLintOnSaveLogs: boolean;
3434
lintOnFileChange: boolean;
3535
displayLintOnFileChangeNotice: boolean;
36-
lintOnFileContentChangeDelay: string;
3736
foldersToIgnore: string[];
3837
filesToIgnore: FileToIgnore[];
3938
linterLocale: string;
@@ -52,7 +51,6 @@ export const DEFAULT_SETTINGS: Partial<LinterSettings> = {
5251
displayChanged: true,
5352
lintOnFileChange: false,
5453
displayLintOnFileChangeNotice: false,
55-
lintOnFileContentChangeDelay: AfterFileChangeLintTimes.Never,
5654
settingsConvertedToConfigKeyValues: false,
5755
foldersToIgnore: [],
5856
filesToIgnore: [],

0 commit comments

Comments
 (0)