Skip to content

Commit

Permalink
v2.7.1 (#57)
Browse files Browse the repository at this point in the history
-   Fix indentation with parentheses ([#25](#25))
  • Loading branch information
mark-wiemer authored Mar 1, 2021
1 parent cd62a8a commit 1ef84ba
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 17 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.7.1 - 2021-02-28

- Fix indentation with parentheses ([#25](https://github.com/mark-wiemer/vscode-autohotkey-plus-plus/issues/25))

## 2.7.0 - 2021-02-21

- Respect user choice to indent with either tabs or spaces ([#49](https://github.com/mark-wiemer/vscode-autohotkey-plus-plus/issues/49))
Expand Down
8 changes: 3 additions & 5 deletions demos/demo_for_ahk_v1.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ function()
if (str == "str") {
MsgBox Overwrite primitive variable!
}
; Known bug: `line <n>` lines should be indented one level more
; https://github.com/mark-wiemer/vscode-autohotkey-plus-plus/issues/25
str_multiline := "
(LTrim
line 1
line 2
line 3
line 1
line 2
line 3
)"
int := 123
float := 123.456
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-autohotkey-plus-plus",
"displayName": "AutoHotkey Plus Plus",
"description": "AutoHotkey IntelliSense, debug, and language support for VS Code, forked from AutoHotkey Plus by cweijan",
"version": "2.7.0",
"version": "2.7.1",
"publisher": "mark-wiemer",
"engines": {
"vscode": "^1.30.0"
Expand Down
26 changes: 24 additions & 2 deletions src/providers/formattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class FormatProvider implements vscode.DocumentFormattingEditProvider {
token: vscode.CancellationToken,
): vscode.TextEdit[] {
let formattedDocument = '';
/** Current level of indentation. 0 = top-level, no indentation */
let depth = 0;
/** ??? */
let tagDepth = 0;
let oneCommandCode = false;
let blockComment = false;
Expand Down Expand Up @@ -102,7 +104,7 @@ export class FormatProvider implements vscode.DocumentFormattingEditProvider {
}
}

// Check open and close braces
// Check close braces
if (purifiedLine.match(/}/) != null) {
let temp = purifiedLine.match(/}/).length;
const t2 = purifiedLine.match(/{[^{}]*}/);
Expand All @@ -114,6 +116,16 @@ export class FormatProvider implements vscode.DocumentFormattingEditProvider {
atTopLevel = false;
}
}

// Check close parens
if (purifiedLine.includes(')')) {
const openCount = purifiedLine.match(/\(/)?.length ?? 0;
const closeCount = purifiedLine.match(/\)/).length;
if (closeCount > openCount) {
depth--;
}
}

if (oneCommandCode && purifiedLine.match(/{/) != null) {
let temp = purifiedLine.match(/{/).length;
const t2 = purifiedLine.match(/{[^{}]*}/);
Expand All @@ -130,10 +142,10 @@ export class FormatProvider implements vscode.DocumentFormattingEditProvider {
depth = 0;
}

// Add the indented line to the file
const indentationChars = options.insertSpaces
? ' '.repeat(depth * options.tabSize)
: '\t'.repeat(depth);

formattedDocument +=
!formattedLine || formattedLine.trim() == ''
? formattedLine
Expand All @@ -157,6 +169,7 @@ export class FormatProvider implements vscode.DocumentFormattingEditProvider {
atTopLevel = false;
}

// Check open braces
if (purifiedLine.match(/{/) != null) {
let temp = purifiedLine.match(/{/).length;
const t2 = purifiedLine.match(/{[^{}]*}/);
Expand All @@ -169,6 +182,15 @@ export class FormatProvider implements vscode.DocumentFormattingEditProvider {
}
}

// Check open parens
if (purifiedLine.includes('(')) {
const openCount = purifiedLine.match(/\(/).length;
const closeCount = purifiedLine.match(/\)/)?.length ?? 0;
if (openCount > closeCount) {
depth++;
}
}

if (purifiedLine.match(/:\s*$/)) {
depth++;
tagDepth = depth;
Expand Down
1 change: 0 additions & 1 deletion src/test/suite/format/demo.in.ahk

This file was deleted.

1 change: 0 additions & 1 deletion src/test/suite/format/demo.out.ahk

This file was deleted.

36 changes: 29 additions & 7 deletions src/test/suite/format/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,35 @@ import * as path from 'path';
import * as vscode from 'vscode';
import { FormatProvider } from '../../../providers/formattingProvider';

// Make sure const values match provided suffixes below
const inFilenameSuffix = '.in.ahk';
const outFilenameSuffix = '.out.ahk';
interface FormatTest {
// Make sure provided suffixes match const values above
/** Name of the file, excluding the suffix (suffixes include .in.ahk, .out.ahk) */
filenameRoot: string;
options: vscode.FormattingOptions;
/** If not provided, file will be formatted with 4 spaces. */
options?: Partial<vscode.FormattingOptions>;
}

/** Default formatting options */
const defaultOptions: vscode.FormattingOptions = {
tabSize: 4,
insertSpaces: true,
};
const formatTests: FormatTest[] = [
{ filenameRoot: 'demo', options: { tabSize: 4, insertSpaces: true } },
{ filenameRoot: 'multiline', options: { tabSize: 4, insertSpaces: false } },
{ filenameRoot: 'demo' },
{
filenameRoot: 'insertSpacesFalse',
options: { insertSpaces: false },
},
{
filenameRoot: 'multilineString',
},
{
filenameRoot: 'tabSize2',
options: { tabSize: 2 },
},
];
const inFilenameSuffix = '.in.ahk';
const outFilenameSuffix = '.out.ahk';

const filesParentPath = path.join(
__dirname,
Expand All @@ -26,6 +44,7 @@ const filesParentPath = path.join(
'test',
'suite',
'format',
'samples',
);

suite('Formatter', () => {
Expand All @@ -45,7 +64,10 @@ suite('Formatter', () => {
const formatter = new FormatProvider();
const edits = formatter.provideDocumentFormattingEdits(
unformattedSampleFile,
formatTest.options,
{
...defaultOptions,
...formatTest.options,
},
null,
);
await textEditor.edit((editBuilder) => {
Expand Down
81 changes: 81 additions & 0 deletions src/test/suite/format/samples/demo.in.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
globalVar := "Global"
global SuperGlobalVar := "SuperGlobal"
function()
return
function()
{
globalVar := "Local"
SuperGlobalVar := "Local"
bool := true
str := "string"
if (str == "str") {
MsgBox Overwrite primitive variable!
}
str_multiline := "
(LTrim
line 1
line 2
line 3
)"
int := 123
float := 123.456

emptyArray := []
smallArray := [1, 2, { str: "string" }]
sparseArray := { 1: 1, 3: 3 }
arrayLike := { 1: 1, 2: 2, 3: 3, length: 3 }
bigArray := []
Loop 150 {
bigArray.push(A_Index)
}
if (bigArray == "str") {
MsgBox Overwrite object variable!
}

obj := { str: str, int: int, float: float }
objobj := { str: str, obj: obj }
objobjobj := { str: str, int: int, obj: { str: str, obj: obj } }

circular := {}
circular.circular := circular
instance := new Cls()

enum := obj._NewEnum()
}
class Cls
{
instanceVar := "instance"
static str := "string"
static num := 123
static obj := { str: "string", int: 123, float: 123.456 }
property[] {
get {
}
}
method() {
}
}

; Block comments and nested regions
/* ;region
Collapse me!
{
Collapse me too!
}
*/ ;endregion

; Function calls (with a space before parens)
foo()
bar ()
baz () ; multiple spaces

; SUBROUTINES

; ExitApp indentation for subroutines
MySub:
foo()
ExitApp ; should not be indented at all

; Formatting line below ternary with third operand a string value
true ? 1 : "string"
foo() ; should not be indented at all
81 changes: 81 additions & 0 deletions src/test/suite/format/samples/demo.out.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
globalVar := "Global"
global SuperGlobalVar := "SuperGlobal"
function()
return
function()
{
globalVar := "Local"
SuperGlobalVar := "Local"
bool := true
str := "string"
if (str == "str") {
MsgBox Overwrite primitive variable!
}
str_multiline := "
(LTrim
line 1
line 2
line 3
)"
int := 123
float := 123.456

emptyArray := []
smallArray := [1, 2, { str: "string" }]
sparseArray := { 1: 1, 3: 3 }
arrayLike := { 1: 1, 2: 2, 3: 3, length: 3 }
bigArray := []
Loop 150 {
bigArray.push(A_Index)
}
if (bigArray == "str") {
MsgBox Overwrite object variable!
}

obj := { str: str, int: int, float: float }
objobj := { str: str, obj: obj }
objobjobj := { str: str, int: int, obj: { str: str, obj: obj } }

circular := {}
circular.circular := circular
instance := new Cls()

enum := obj._NewEnum()
}
class Cls
{
instanceVar := "instance"
static str := "string"
static num := 123
static obj := { str: "string", int: 123, float: 123.456 }
property[] {
get {
}
}
method() {
}
}

; Block comments and nested regions
/* ;region
Collapse me!
{
Collapse me too!
}
*/ ;endregion

; Function calls (with a space before parens)
foo()
bar ()
baz () ; multiple spaces

; SUBROUTINES

; ExitApp indentation for subroutines
MySub:
foo()
ExitApp ; should not be indented at all

; Formatting line below ternary with third operand a string value
true ? 1 : "string"
foo() ; should not be indented at all
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions src/test/suite/format/samples/multilineString.in.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x := "
(LTrim
hello world
)
"
5 changes: 5 additions & 0 deletions src/test/suite/format/samples/multilineString.out.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x := "
(LTrim
hello world
)
"
3 changes: 3 additions & 0 deletions src/test/suite/format/samples/tabSize2.in.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo() {
x := 1
}
3 changes: 3 additions & 0 deletions src/test/suite/format/samples/tabSize2.out.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo() {
x := 1
}
1 change: 1 addition & 0 deletions src/test/suite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function run(): Promise<void> {
ui: 'tdd',
});
(mocha as any).color();
mocha.timeout(0);

const testsRoot = path.resolve(__dirname, '..');

Expand Down

0 comments on commit 1ef84ba

Please sign in to comment.