Skip to content

Commit

Permalink
Merge pull request #1189 from pjkaufman/master
Browse files Browse the repository at this point in the history
Hide Unreachable Settings
  • Loading branch information
pjkaufman authored Oct 10, 2024
2 parents 64a94f5 + b253753 commit ec657ea
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 62 deletions.
8 changes: 4 additions & 4 deletions src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ export default {
'name': 'Display Lint on File Change Message',
'description': 'Displays a message when `Lint on Focused File Change` occurs',
},
'timestamp-update-on-file-contents-updated': {
'name': 'Update YAML Timestamp on File Contents Update',
'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.',
},
'folders-to-ignore': {
'name': 'Folders to ignore',
'description': 'Folders to ignore when linting all files or linting on save.',
Expand Down Expand Up @@ -849,6 +845,10 @@ export default {
'name': 'Convert Local Time to UTC',
'description': 'Uses UTC equivalent for saved dates instead of local time',
},
'update-on-file-contents-updated': {
'name': 'Update YAML Timestamp on File Contents Update',
'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.',
},
},
// yaml-title-alias.ts
'yaml-title-alias': {
Expand Down
32 changes: 26 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,16 @@ export default class LinterPlugin extends Plugin {
setLogLevel(this.settings.logLevel);
await this.setOrUpdateMomentInstance();

let updateMade = false;
if (!this.settings.settingsConvertedToConfigKeyValues) {
this.moveConfigValuesToKeyBasedFormat();
updateMade = await this.moveConfigValuesToKeyBasedFormat();
}

if ('lintOnFileContentChangeDelay' in this.settings) {
this.settings.ruleConfigs['yaml-timestamp']['update-on-file-contents-updated'] = this.settings['lintOnFileContentChangeDelay'];

delete this.settings['lintOnFileContentChangeDelay'];
updateMade = true;
}

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

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


delete this.settings.ruleConfigs[rule.alias]['force-retention-of-create-value'];
updateMade = true;
}

if (!('date-modified-source-of-truth' in this.settings.ruleConfigs[rule.alias])) {
this.settings.ruleConfigs[rule.alias]['date-modified-source-of-truth'] = defaults['date-modified-source-of-truth'];
updateMade = true;
}
}
}

this.updatePasteOverrideStatus();
this.updateHasCustomCommandStatus();

if (updateMade) {
await this.saveSettings();
}
}

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

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

Expand Down Expand Up @@ -610,7 +625,7 @@ export default class LinterPlugin extends Plugin {

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

private moveConfigValuesToKeyBasedFormat() {
private async moveConfigValuesToKeyBasedFormat(): Promise<boolean> {
setLanguage('en');

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

this.settings.ruleConfigs[rule.alias] = newSettingValues;
delete this.settings.ruleConfigs[ruleName];

updateMade = true;
}
}

this.settings.settingsConvertedToConfigKeyValues = true;
void this.saveSettings();
await this.saveSettings();

setLanguage(window.localStorage.getItem('language'));

return updateMade;
}

private getAllFilesInFolder(startingFolder: TFolder): TFile[] {
Expand Down
63 changes: 46 additions & 17 deletions src/option.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Setting} from 'obsidian';
import {getTextInLanguage, LanguageStringKey} from './lang/helpers';
import LinterPlugin from './main';
import {parseTextToHTMLWithoutOuterParagraph} from './ui/helpers';
import {hideEl, parseTextToHTMLWithoutOuterParagraph, unhideEl} from './ui/helpers';
import {LinterSettings} from './settings-data';
import {AutoCorrectFilesPickerOption} from './ui/linter-components/auto-correct-files-picker-option';

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

export abstract class Option {
public ruleAlias: string;
protected setting: Setting;

/**
* Create an option
Expand Down Expand Up @@ -43,38 +44,55 @@ export abstract class Option {
settings.ruleConfigs[this.ruleAlias][this.configKey] = value;
}

protected parseNameAndDescriptionAndRemoveSettingBorder(setting: Setting, plugin: LinterPlugin) {
parseTextToHTMLWithoutOuterParagraph(plugin.app, this.getName(), setting.nameEl, plugin.settingsTab.component);
parseTextToHTMLWithoutOuterParagraph(plugin.app, this.getDescription(), setting.descEl, plugin.settingsTab.component);
protected parseNameAndDescriptionAndRemoveSettingBorder(plugin: LinterPlugin) {
parseTextToHTMLWithoutOuterParagraph(plugin.app, this.getName(), this.setting.nameEl, plugin.settingsTab.component);
parseTextToHTMLWithoutOuterParagraph(plugin.app, this.getDescription(), this.setting.descEl, plugin.settingsTab.component);

setting.settingEl.addClass('linter-no-border');
setting.descEl.addClass('linter-no-padding-top');
this.setting.settingEl.addClass('linter-no-border');
this.setting.descEl.addClass('linter-no-padding-top');
}

hide() {
hideEl(this.setting.settingEl);
}

unhide() {
unhideEl(this.setting.settingEl);
}
}

export class BooleanOption extends Option {
public defaultValue: boolean;

constructor(configKey: string, nameKey: LanguageStringKey, descriptionKey: LanguageStringKey, defaultValue: any, ruleAlias?: string | null, private onChange?: (value: boolean) => void) {
super(configKey, nameKey, descriptionKey, defaultValue, ruleAlias);
}

public display(containerEl: HTMLElement, settings: LinterSettings, plugin: LinterPlugin): void {
const setting = new Setting(containerEl)
this.setting = new Setting(containerEl)
.addToggle((toggle) => {
toggle.setValue(settings.ruleConfigs[this.ruleAlias][this.configKey]);
toggle.onChange((value) => {
this.setOption(value, settings);
plugin.settings = settings;

if (this.onChange) {
this.onChange(value);
}

void plugin.saveSettings();
});
});

this.parseNameAndDescriptionAndRemoveSettingBorder(setting, plugin);
this.parseNameAndDescriptionAndRemoveSettingBorder(plugin);
}
}

export class TextOption extends Option {
public defaultValue: string;

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

this.parseNameAndDescriptionAndRemoveSettingBorder(setting, plugin);
this.parseNameAndDescriptionAndRemoveSettingBorder(plugin);
}
}

export class TextAreaOption extends Option {
public defaultValue: string;

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

this.parseNameAndDescriptionAndRemoveSettingBorder(setting, plugin);
this.parseNameAndDescriptionAndRemoveSettingBorder(plugin);
}
}

export class MomentFormatOption extends Option {
public defaultValue: boolean;

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

this.parseNameAndDescriptionAndRemoveSettingBorder(setting, plugin);
this.parseNameAndDescriptionAndRemoveSettingBorder(plugin);
}
}

Expand Down Expand Up @@ -153,7 +171,7 @@ export class DropdownOption extends Option {
}

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

this.parseNameAndDescriptionAndRemoveSettingBorder(setting, plugin);
this.parseNameAndDescriptionAndRemoveSettingBorder(plugin);
}
}


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

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

new AutoCorrectFilesPickerOption(containerEl, plugin.settingsTab.component, settings.ruleConfigs[this.ruleAlias][this.configKey], plugin.app, () => {
this.settingEl = containerEl.createDiv();

new AutoCorrectFilesPickerOption(this.settingEl, plugin.settingsTab.component, settings.ruleConfigs[this.ruleAlias][this.configKey], plugin.app, () => {
void plugin.saveSettings();
}, this.nameKey, this.descriptionKey);
}

override hide() {
hideEl(this.settingEl);
}

override unhide() {
unhideEl(this.settingEl);
}
}
12 changes: 11 additions & 1 deletion src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ export class Rule {
) {
this.ruleHeading = this.getName().toLowerCase().replaceAll(' ', '-');

options.unshift(new BooleanOption('enabled', this.descriptionKey, '' as LanguageStringKey, false));
options.unshift(new BooleanOption('enabled', this.descriptionKey, '' as LanguageStringKey, false, alias, (value: boolean) => {
if (options.length > 1) {
for (let i = 1; i < options.length; i++) {
if (value) {
options[i].unhide();
} else {
options[i].hide();
}
}
}
}));
for (const option of options) {
option.ruleAlias = alias;
}
Expand Down
37 changes: 37 additions & 0 deletions src/rules/yaml-timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {escapeDollarSigns} from '../utils/regex';
import {insert} from '../utils/strings';
import parseFormat from 'moment-parseformat';
import {getTextInLanguage} from '../lang/helpers';
import {AfterFileChangeLintTimes} from '../settings-data';

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

convertToUTC?: boolean = false;

// This is not used in the rule itself. It is used for running this rule as a standalone when
// editor content is updated.
timestampUpdateOnFileContentUpdated?: AfterFileChangeLintTimes =AfterFileChangeLintTimes.Never;

@RuleBuilder.noSettingControl()
fileModifiedTime?: string;

Expand Down Expand Up @@ -455,6 +460,38 @@ export default class YamlTimestamp extends RuleBuilder<YamlTimestampOptions> {
descriptionKey: 'rules.yaml-timestamp.convert-to-utc.description',
optionsKey: 'convertToUTC',
}),
new DropdownOptionBuilder<YamlTimestampOptions, AfterFileChangeLintTimes>({
OptionsClass: YamlTimestampOptions,
nameKey: 'rules.yaml-timestamp.update-on-file-contents-updated.name',
descriptionKey: 'rules.yaml-timestamp.update-on-file-contents-updated.description',
optionsKey: 'timestampUpdateOnFileContentUpdated',
records: [
{
value: AfterFileChangeLintTimes.Never,
description: AfterFileChangeLintTimes.Never,
},
{
value: AfterFileChangeLintTimes.After5Seconds,
description: AfterFileChangeLintTimes.After5Seconds,
},
{
value: AfterFileChangeLintTimes.After10Seconds,
description: AfterFileChangeLintTimes.After10Seconds,
},
{
value: AfterFileChangeLintTimes.After15Seconds,
description: AfterFileChangeLintTimes.After15Seconds,
},
{
value: AfterFileChangeLintTimes.After30Seconds,
description: AfterFileChangeLintTimes.After30Seconds,
},
{
value: AfterFileChangeLintTimes.After1Minute,
description: AfterFileChangeLintTimes.After1Minute,
},
],
}),
];
}
}
2 changes: 0 additions & 2 deletions src/settings-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export interface LinterSettings {
recordLintOnSaveLogs: boolean;
lintOnFileChange: boolean;
displayLintOnFileChangeNotice: boolean;
lintOnFileContentChangeDelay: string;
foldersToIgnore: string[];
filesToIgnore: FileToIgnore[];
linterLocale: string;
Expand All @@ -52,7 +51,6 @@ export const DEFAULT_SETTINGS: Partial<LinterSettings> = {
displayChanged: true,
lintOnFileChange: false,
displayLintOnFileChangeNotice: false,
lintOnFileContentChangeDelay: AfterFileChangeLintTimes.Never,
settingsConvertedToConfigKeyValues: false,
foldersToIgnore: [],
filesToIgnore: [],
Expand Down
Loading

0 comments on commit ec657ea

Please sign in to comment.