From d24e48052c48b5b2b895d49ee4db69e220e121ef Mon Sep 17 00:00:00 2001 From: Kyklish Date: Sat, 25 May 2024 02:02:37 +0300 Subject: [PATCH] `FIX`: for #433 PR (single line hotkey fall-through scenario) (#441) * reset tagDepth in IF-DIRECTIVE * comments * add tests * comments * reset tagDepth in single line hotkey --- src/providers/format.test.ts | 2 ++ src/providers/formattingProvider.ts | 33 +++++++++++-------- ...ingle-line-hotkey-with-if-directive.in.ahk | 6 ++++ ...ngle-line-hotkey-with-if-directive.out.ahk | 6 ++++ ...gh-single-line-hotkey-with-function.in.ahk | 7 ++++ ...h-single-line-hotkey-with-function.out.ahk | 7 ++++ 6 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 src/providers/samples/440-fall-through-single-line-hotkey-with-if-directive.in.ahk create mode 100644 src/providers/samples/440-fall-through-single-line-hotkey-with-if-directive.out.ahk create mode 100644 src/providers/samples/442-fall-through-single-line-hotkey-with-function.in.ahk create mode 100644 src/providers/samples/442-fall-through-single-line-hotkey-with-function.out.ahk diff --git a/src/providers/format.test.ts b/src/providers/format.test.ts index d80a4154..c804f1fc 100644 --- a/src/providers/format.test.ts +++ b/src/providers/format.test.ts @@ -71,6 +71,8 @@ const formatTests: FormatTest[] = [ { filenameRoot: '316-if-object-continuation-section' }, { filenameRoot: '429-single-line-hotkey' }, { filenameRoot: '432-label-inside-code-block' }, + { filenameRoot: '440-fall-through-single-line-hotkey-with-if-directive' }, + { filenameRoot: '442-fall-through-single-line-hotkey-with-function' }, { filenameRoot: 'ahk-explorer' }, { filenameRoot: 'align-assignment' }, { filenameRoot: 'demo' }, diff --git a/src/providers/formattingProvider.ts b/src/providers/formattingProvider.ts index 7d89dd6c..0cefdb2b 100644 --- a/src/providers/formattingProvider.ts +++ b/src/providers/formattingProvider.ts @@ -38,21 +38,19 @@ export const internalFormat = ( /** Level of indentation on previous line */ let prevLineDepth = 0; /** - * It's marker for `Return`, `ExitApp`, `#Directive` commands and `Labels`, + * It's marker for `Return`, `ExitApp`, `Hotkeys` and `Labels`, * which allow/disallow for them to be un-indented. * * ------------------------------------------------------------------------- * `tagDepth === 0`: * - * Indentation level was decreased by `Return` or `ExitApp` command, + * Indentation level was decreased by `Return`, `ExitApp`, `#If Directive`, * so they placed on same indentation level as `Label`. * * Decrement of indentation level by `Label` is disallowed (previous * `Label` finished with `Return` or `ExitApp` command and un-indent for * fall-through scenario not needed). * - * Decrement indentation by one level for `#Directive` is allowed. - * * ------------------------------------------------------------------------- * `tagDepth === depth`: * @@ -75,7 +73,7 @@ export const internalFormat = ( * ------------------------------------------------------------------------- * `tagDepth > 0` : * - * `#Directive` allowed to be un-indented by `tagDepth` value (jump + * `#If Directive` allowed to be un-indented by `tagDepth` value (jump * several indentation levels). * * ------------------------------------------------------------------------- @@ -85,6 +83,12 @@ export const internalFormat = ( * * `Case:` and `Default:` must not make syncing to disallow `Return`, * `ExitApp` and `Label` to un-indent inside `Switch-Case` block. + * + * ------------------------------------------------------------------------- + * `tagDepth = 0`: + * + * `Return`, `ExitApp`, `#If Directive`, `HotkeySingleLine` resets + * `tagDepth` value, when they un-indented. */ let tagDepth = 0; @@ -305,8 +309,8 @@ export const internalFormat = ( */ const hotkeySingleLine = /^.+::/; /** - * `#Directive`, that will create context-sensitive hotkeys and hotstrings. - * Example of `#Directives`: + * `#IF Directive`, that will create context-sensitive hotkeys and hotstrings. + * Example of `#If Directives`: * ```ahk * #IfWinActive WinTitle * #IfWinNotActive WinTitle @@ -692,12 +696,12 @@ export const internalFormat = ( } } - // #DIRECTIVE + // #IF DIRECTIVE // #IfWinActive WinTitle1 // Hotkey:: - // #IfWinActive WinTitle2 <-- fall-through scenario for #Directive with - // Hotkey:: parameters - // #If <-- de-indent #Directive without parameters + // #IfWinActive WinTitle2 <-- fall-through scenario for #IF DIRECTIVE + // Hotkey:: with parameters + // #If <-- de-indent #IF DIRECTIVE without parameters if (purifiedLine.match('^' + sharpDirective + '\\b')) { if (tagDepth > 0) { depth -= tagDepth; @@ -849,14 +853,15 @@ export const internalFormat = ( openBraceIndent = false; } - // #DIRECTIVE with parameters - // #If Expression <-- indent next line after '#Directive' + // #IF DIRECTIVE with parameters + // #If Expression <-- indent next line after '#IF DIRECTIVE' // F1:: MsgBox Help if ( purifiedLine.match('^' + sharpDirective + '\\b.+') && indentCodeAfterIfDirective ) { depth++; + tagDepth = 0; } // SWITCH-CASE-DEFAULT or LABEL: or HOTKEY:: @@ -876,6 +881,8 @@ export const internalFormat = ( tagDepth = depth; } } + } else if (purifiedLine.match(hotkeySingleLine)) { + tagDepth = 0; } // CONTINUATION SECTION: Expression, Object diff --git a/src/providers/samples/440-fall-through-single-line-hotkey-with-if-directive.in.ahk b/src/providers/samples/440-fall-through-single-line-hotkey-with-if-directive.in.ahk new file mode 100644 index 00000000..6afecfaa --- /dev/null +++ b/src/providers/samples/440-fall-through-single-line-hotkey-with-if-directive.in.ahk @@ -0,0 +1,6 @@ +; [Issue #440](https://github.com/mark-wiemer/vscode-autohotkey-plus-plus/issues/440) +F1:: +F2::code +#IfWinActive, WinTitle +F3::code +#If diff --git a/src/providers/samples/440-fall-through-single-line-hotkey-with-if-directive.out.ahk b/src/providers/samples/440-fall-through-single-line-hotkey-with-if-directive.out.ahk new file mode 100644 index 00000000..dc93de1d --- /dev/null +++ b/src/providers/samples/440-fall-through-single-line-hotkey-with-if-directive.out.ahk @@ -0,0 +1,6 @@ +; [Issue #440](https://github.com/mark-wiemer/vscode-autohotkey-plus-plus/issues/440) +F1:: +F2::code +#IfWinActive, WinTitle + F3::code +#If diff --git a/src/providers/samples/442-fall-through-single-line-hotkey-with-function.in.ahk b/src/providers/samples/442-fall-through-single-line-hotkey-with-function.in.ahk new file mode 100644 index 00000000..96232598 --- /dev/null +++ b/src/providers/samples/442-fall-through-single-line-hotkey-with-function.in.ahk @@ -0,0 +1,7 @@ +; [Issue #442](https://github.com/mark-wiemer/vscode-autohotkey-plus-plus/issues/442) +F1:: +F2::code +foo() { +code +Return +} diff --git a/src/providers/samples/442-fall-through-single-line-hotkey-with-function.out.ahk b/src/providers/samples/442-fall-through-single-line-hotkey-with-function.out.ahk new file mode 100644 index 00000000..d24b7fa1 --- /dev/null +++ b/src/providers/samples/442-fall-through-single-line-hotkey-with-function.out.ahk @@ -0,0 +1,7 @@ +; [Issue #442](https://github.com/mark-wiemer/vscode-autohotkey-plus-plus/issues/442) +F1:: +F2::code +foo() { + code + Return +}