Skip to content

Commit

Permalink
Fix indent after Hotkey:: and optimize #Directive code (#304)
Browse files Browse the repository at this point in the history
Closes #303
  • Loading branch information
kyklish authored Jan 18, 2023
1 parent 664dd75 commit 80d66ba
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 69 deletions.
77 changes: 28 additions & 49 deletions src/providers/formattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,6 @@ export const internalFormat = (
*/
let openBraceObjectDepth = -1;

// OTHER
/**
* This line is `#Directive`, that will create context-sensitive hotkeys
* and hotstrings.
* Example of `#Directives`:
* ```ahk
* #IfWinActive WinTitle
* #IfWinNotActive WinTitle
* #IfWinExist WinTitle
* #IfWinNotExist WinTitle
* #If Expression
* ```
*/
let sharpDirectiveLine = false;

// BLOCK COMMENT
/** This line is block comment */
let blockComment = false;
Expand Down Expand Up @@ -306,6 +291,12 @@ export const internalFormat = (
* Example: `Label:`
*/
const label = /^[^\s\t,`]+:$/;
/**
* Hotkey and hotstring without code after it.
*
* Example: `F1 & F2 Up::` (hotkey), `::btw::` (hotstring)
*/
const hotkey = /^.*::$/;
/**
* `#Directive`, that will create context-sensitive hotkeys and hotstrings.
* Example of `#Directives`:
Expand Down Expand Up @@ -340,7 +331,6 @@ export const internalFormat = (
const emptyLine = purifiedLine === '';

detectOneCommandCode = true;
sharpDirectiveLine = false;

const openBraceNum = braceNumber(purifiedLine, '{');
const closeBraceNum = braceNumber(purifiedLine, '}');
Expand Down Expand Up @@ -695,33 +685,17 @@ export const internalFormat = (
}
}

// #DIRECTIVE without parameters
// #IfWinActive WinTitle
// Hotkey::
// #If <-- de-indent #Directive without parameters
if (purifiedLine.match('^' + sharpDirective + '$')) {
if (indentCodeAfterSharpDirective) {
if (tagDepth > 0) {
depth -= tagDepth;
} else {
depth--;
}
}
}

// #DIRECTIVE with parameters
// #DIRECTIVE
// #IfWinActive WinTitle1
// Hotkey::
// #IfWinActive WinTitle2 <-- fall-through scenario for #Directive with
// Hotkey:: parameters
if (purifiedLine.match('^' + sharpDirective + '\\b.+')) {
if (indentCodeAfterSharpDirective) {
if (tagDepth > 0) {
depth -= tagDepth;
} else {
depth--;
}
sharpDirectiveLine = true;
// #If <-- de-indent #Directive without parameters
if (purifiedLine.match('^' + sharpDirective + '\\b')) {
if (tagDepth > 0) {
depth -= tagDepth;
} else {
depth--;
}
}

Expand All @@ -737,13 +711,13 @@ export const internalFormat = (
depth--;
}

// SWITCH-CASE-DEFAULT or LABEL:
// SWITCH-CASE-DEFAULT or LABEL: or HOTKEY::
if (purifiedLine.match(switchCaseDefault)) {
// Case: or Default:
depth--;
} else if (purifiedLine.match(label)) {
// Label:
} else if (purifiedLine.match(label) || purifiedLine.match(hotkey)) {
if (indentCodeAfterLabel) {
// Label: or Hotkey::
// De-indent label or hotkey, if they not end with 'return'
// command.
// This is fall-through scenario. Example:
Expand Down Expand Up @@ -864,22 +838,27 @@ export const internalFormat = (
// #DIRECTIVE with parameters
// #If Expression <-- indent next line after '#Directive'
// F1:: MsgBox Help
if (sharpDirectiveLine && indentCodeAfterSharpDirective) {
if (
purifiedLine.match('^' + sharpDirective + '\\b.+') &&
indentCodeAfterSharpDirective
) {
depth++;
}

// SWITCH-CASE-DEFAULT or LABEL:
// SWITCH-CASE-DEFAULT or LABEL: or HOTKEY::
if (purifiedLine.match(switchCaseDefault)) {
// Case: or Default: <-- indent next line
// code
depth++;
// Do not sync here 'tagDepth' with 'depth' to prevent 'Return' and
// 'ExitApp' to de-indent inside 'Switch-Case-Default' construction!
} else if (purifiedLine.match(label) && indentCodeAfterLabel) {
// Label: <-- indent next line
// code
depth++;
tagDepth = depth;
} else if (purifiedLine.match(label) || purifiedLine.match(hotkey)) {
if (indentCodeAfterLabel) {
// Label: or Hotkey:: <-- indent next line
// code
depth++;
tagDepth = depth;
}
}

// CONTINUATION SECTION: Expression, Object
Expand Down
1 change: 1 addition & 0 deletions src/test/suite/format/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const formatTests: FormatTest[] = [
options: { insertSpaces: false },
},
{ filenameRoot: 'legacy-text-sharp-directive' },
{ filenameRoot: 'label-combination' },
{ filenameRoot: 'label-fall-through' },
{ filenameRoot: 'label-specific-name' },
{ filenameRoot: 'return-exit-exitapp' },
Expand Down
10 changes: 10 additions & 0 deletions src/test/suite/format/samples/hotstring.in.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
::btw::by the way
code

::btw::
code
return

:*b0:<em>::
code
return
10 changes: 10 additions & 0 deletions src/test/suite/format/samples/hotstring.out.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
::btw::by the way
code

::btw::
code
return

:*b0:<em>::
code
return
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Label1:
Label:
MsgBox
Label2:
SoundBeep
return
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Label1:
Label:
MsgBox
Label2:
SoundBeep
return
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Label1:
Label:
MsgBox
Label2:
SoundBeep
return
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Label1:
Label:
MsgBox
Label2:
SoundBeep
return
18 changes: 18 additions & 0 deletions src/test/suite/format/samples/label-combination.in.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
F1::code
code

!^+F1::
code
return

F1 Up::
code
return

F1 & F2::
code
return

F1 & F2 Up::
code
return
18 changes: 18 additions & 0 deletions src/test/suite/format/samples/label-combination.out.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
F1::code
code

!^+F1::
code
return

F1 Up::
code
return

F1 & F2::
code
return

F1 & F2 Up::
code
return
7 changes: 3 additions & 4 deletions src/test/suite/format/samples/label-specific-name.in.ahk
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
GoTo, If
ExitApp
If:
GoTo Loop
return

Else:
GoTo Loop
If:
GoTo Loop
return

Loop:
Expand Down
7 changes: 3 additions & 4 deletions src/test/suite/format/samples/label-specific-name.out.ahk
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
GoTo, If
ExitApp
If:
GoTo Loop
return

Else:
GoTo Loop
If:
GoTo Loop
return

Loop:
Expand Down

0 comments on commit 80d66ba

Please sign in to comment.