Skip to content

Commit

Permalink
FIX: for #433 PR (single line hotkey fall-through scenario) (#441)
Browse files Browse the repository at this point in the history
* reset tagDepth in IF-DIRECTIVE

* comments

* add tests

* comments

* reset tagDepth in single line hotkey
  • Loading branch information
kyklish authored May 24, 2024
1 parent ced2c5f commit d24e480
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/providers/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
33 changes: 20 additions & 13 deletions src/providers/formattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
*
Expand All @@ -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).
*
* -------------------------------------------------------------------------
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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::
Expand All @@ -876,6 +881,8 @@ export const internalFormat = (
tagDepth = depth;
}
}
} else if (purifiedLine.match(hotkeySingleLine)) {
tagDepth = 0;
}

// CONTINUATION SECTION: Expression, Object
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; [Issue #442](https://github.com/mark-wiemer/vscode-autohotkey-plus-plus/issues/442)
F1::
F2::code
foo() {
code
Return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; [Issue #442](https://github.com/mark-wiemer/vscode-autohotkey-plus-plus/issues/442)
F1::
F2::code
foo() {
code
Return
}

0 comments on commit d24e480

Please sign in to comment.