Skip to content

Commit

Permalink
fix indent for single-line hotkey in fall-through scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
kyklish committed May 14, 2024
1 parent 14b1114 commit 718c76e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/providers/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const formatTests: FormatTest[] = [
{ filenameRoot: '290-ifmsgbox' },
{ filenameRoot: '291-single-line-comment' },
{ filenameRoot: '316-if-object-continuation-section' },
{ filenameRoot: '429-single-line-hotkey' },
{ filenameRoot: 'ahk-explorer' },
{ filenameRoot: 'align-assignment' },
{ filenameRoot: 'demo' },
Expand Down
35 changes: 24 additions & 11 deletions src/providers/formattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const internalFormat = (
/** Level of indentation on previous line */
let prevLineDepth = 0;
/**
* It's marker for `Return`, `ExitApp`, `#Directive` commands, which
* allow/disallow for them to be un-indented.
* It's marker for `Return`, `ExitApp`, `#Directive` commands and `Labels`,
* which allow/disallow for them to be un-indented.
*
* -------------------------------------------------------------------------
* `tagDepth === 0`:
Expand All @@ -59,15 +59,15 @@ export const internalFormat = (
* Current indentation level is in sync with `Label` indentation level
* (no additional indent added by block `{}`, `oneCommandCode`, etc...).
*
* `Return` or `ExitApp` commands allowed to be un-indented, so they will
* be placed on same indentation level as `Label`.
* `Return`, `ExitApp`, `Fall-Through Label` allowed to be un-indented,
* so they will be placed on same indentation level as `Label`.
*
* `Label` allowed to be un-indented for fall-through scenario.
*
* -------------------------------------------------------------------------
* `tagDepth !== depth`:
*
* `Return` or `ExitApp` commands disallowed to be un-indented, so they
* `Return`, `ExitApp`, `Label` disallowed to be un-indented, so they
* will obey indentation rules as code above them (`Return` inside
* function, block `{}`, `oneCommandCode`, etc... stay on same
* indentation level as code above them).
Expand All @@ -83,8 +83,8 @@ export const internalFormat = (
*
* Only `Label` makes syncing `tagDepth` with `depth`.
*
* `Case:` and `Default:` must not make syncing to disallow `Return` and
* `ExitApp` un-indent inside `Switch-Case` block.
* `Case:` and `Default:` must not make syncing to disallow `Return`,
* `ExitApp` and `Label` to un-indent inside `Switch-Case` block.
*/
let tagDepth = 0;

Expand Down Expand Up @@ -293,11 +293,17 @@ export const internalFormat = (
*/
const label = /^[^\s\t,`]+(?<!:):$/;
/**
* Hotkey and hotstring without code after it.
* Hotkey or hotstring without code after it.
*
* Example: `F1 & F2 Up::` (hotkey), `::btw::` (hotstring)
*/
const hotkey = /^.+::$/;
/**
* Hotkey or hotstring with code on same line.
*
* Example: `#n::Run Notepad` (hotkey), `::btw::by the way` (hotstring)
*/
const hotkeySingleLine = /^.+::/;
/**
* `#Directive`, that will create context-sensitive hotkeys and hotstrings.
* Example of `#Directives`:
Expand Down Expand Up @@ -716,7 +722,11 @@ export const internalFormat = (
if (purifiedLine.match(switchCaseDefault)) {
// Case: or Default:
depth--;
} else if (purifiedLine.match(label) || purifiedLine.match(hotkey)) {
} else if (
purifiedLine.match(label) ||
purifiedLine.match(hotkey) ||
purifiedLine.match(hotkeySingleLine)
) {
if (indentCodeAfterLabel) {
// Label: or Hotkey::
// De-indent label or hotkey, if they not end with 'return'
Expand All @@ -727,8 +737,11 @@ export const internalFormat = (
// Label2: <-- de-indent
// code
// return
// No need to make 'tagDepth' in sync with 'depth', 'Label'
// check for next line will do it.
// De-indent single-line hotkey, after label.
// This is fall-through scenario. Example:
// F1::
// F2:: <-- de-indent
// F3:: foo() <-- de-indent
if (tagDepth === depth) {
depth--;
}
Expand Down
16 changes: 16 additions & 0 deletions src/providers/samples/429-single-line-hotkey.in.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; [Issue #429](https://github.com/mark-wiemer/vscode-autohotkey-plus-plus/issues/429)
#IfWinActive, WinTitle
F1::
F2::
code
return

F1::
F2:: foo()
F3:: bar()

F1::
code
F2:: foo()
F3:: bar()
#If
16 changes: 16 additions & 0 deletions src/providers/samples/429-single-line-hotkey.out.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; [Issue #429](https://github.com/mark-wiemer/vscode-autohotkey-plus-plus/issues/429)
#IfWinActive, WinTitle
F1::
F2::
code
return

F1::
F2:: foo()
F3:: bar()

F1::
code
F2:: foo()
F3:: bar()
#If

0 comments on commit 718c76e

Please sign in to comment.