From 4796b7d4d04d1e33b55425eaf1f286a36ceea589 Mon Sep 17 00:00:00 2001 From: petoncle Date: Sun, 22 Oct 2023 23:37:13 +0200 Subject: [PATCH] Add hint flag for specifying a selector that also keeps default hints Fixes #4780. --- src/excmds.ts | 1 + src/lib/hint_util.ts | 36 ++++++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/excmds.ts b/src/excmds.ts index bdc844cf..9087e801 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -5125,6 +5125,7 @@ const KILL_STACK: Element[] = [] - -c [selector] hint links that match the css selector - `bind ;c hint -c [class*="expand"],[class*="togg"]` works particularly well on reddit and HN - this works with most other hint modes, with the caveat that if other hint mode takes arguments your selector must contain no spaces, i.e. `hint -c[yourOtherFlag] [selector] [your other flag's arguments, which may contain spaces]` + - -C [selector] like `-c [selector]` but also hints all elements that would normally be hinted given the other options selected - -x [selector] exclude the matched elements from hinting - -f [text] hint links and inputs that display the given text - `bind hint -f Edit` diff --git a/src/lib/hint_util.ts b/src/lib/hint_util.ts index 8e208dfc..9dee025a 100644 --- a/src/lib/hint_util.ts +++ b/src/lib/hint_util.ts @@ -66,6 +66,7 @@ export class HintConfig implements HintOptions { public pipeAttribute = null public selectors = [] public selectorsExclude = [] + public includeDefaultHintables = true public warnings = [] public static parse(args: string[]): HintConfig { @@ -85,6 +86,8 @@ export class HintConfig implements HintOptions { const result = new HintConfig() const multiLetterFlags = ["fr", "wp", "br", "pipe"] + let cFlagPresent = false + let CFlagPresent = false // Parser state let state = State.Initial @@ -151,6 +154,13 @@ export class HintConfig implements HintOptions { newState = State.ExpectExcmd break case "c": + cFlagPresent = true + result.includeDefaultHintables = false + newState = State.ExpectSelector + break + case "C": + CFlagPresent = true + result.includeDefaultHintables = true newState = State.ExpectSelector break case "x": @@ -238,6 +248,14 @@ export class HintConfig implements HintOptions { result.openMode = newOpenMode } + if (cFlagPresent && CFlagPresent) { + result.warnings.push( + "mutually exclusive -c and -C flags are both specified, last wins, " + + "default hints will " + + (result.includeDefaultHintables ? "be" : "not be") + " included", + ) + } + // Check state changes if (newState !== undefined) { // Some state transitions are dubious, specifically all the ones that go from (argument @@ -402,16 +420,14 @@ export class HintConfig implements HintOptions { } public hintables() { - // User selectors always override default built-ins - const hintables = - this.selectors.length > 0 - ? hinting.hintables( - this.selectors.join(" "), - this.jshints, - this.includeInvisible, - ) - : this.defaultHintables() - + let hintables = this.includeDefaultHintables ? this.defaultHintables() : [] + if (this.selectors.length > 0) { + hintables = hintables.concat(hinting.hintables( + this.selectors.join(" "), + this.jshints, + this.includeInvisible, + )) + } const textFilter = this.textFilter const exclude = this.selectorsExclude.join(" ") for (const elements of hintables) {