-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #285 from BalticAmadeus/283-arrayaccess-formatter
285 Implement ArrayAccessFormatter
- Loading branch information
Showing
21 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
message a[ 3 ]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
message a[3]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
message a[ b[ i / 3 ] + 1]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
message a[b[i / 3] + 1]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
a [ b [ myFunc ( i ) ] ] = d [ e [ f [ i ] ] - g [ j ] ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
a[b[myFunc ( i )]] = d[e[f[i]] - g[j]] |
4 changes: 4 additions & 0 deletions
4
resources/functionalTests/arrayAccess/4-array-literal/input.p
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
VAR INT[4] myArray = [ 10,11,12,13 ]. |
4 changes: 4 additions & 0 deletions
4
resources/functionalTests/arrayAccess/4-array-literal/target.p
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
VAR INT[4] myArray = [10,11,12,13]. |
6 changes: 6 additions & 0 deletions
6
resources/functionalTests/arrayAccess/4-array-literal2/input.p
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
DEFINE VARIABLE myArray AS INTEGER EXTENT 5 NO-UNDO INITIAL [ 10 , 11 , 12 , 13 , 14 ]. | ||
|
||
DISPLAY myArray [ 1 FOR 5 ]. |
6 changes: 6 additions & 0 deletions
6
resources/functionalTests/arrayAccess/4-array-literal2/target.p
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
define VARIABLE myArray AS INTEGER EXTENT 5 NO-UNDO INITIAL [10,11,12,13,14]. | ||
|
||
DISPLAY myArray[1 FOR 5]. |
5 changes: 5 additions & 0 deletions
5
resources/functionalTests/arrayAccess/4-array-literal3-add-space/input.p
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true, | ||
"AblFormatter.arrayAccessFormattingAddSpaceAfterComma": "Yes"}*/ | ||
|
||
VAR INT[] monthlySalesQuotas = [100500,125000,125000,175000,150000,155000,145000]. |
5 changes: 5 additions & 0 deletions
5
resources/functionalTests/arrayAccess/4-array-literal3-add-space/target.p
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true, | ||
"AblFormatter.arrayAccessFormattingAddSpaceAfterComma": "Yes"}*/ | ||
|
||
VAR INT[] monthlySalesQuotas = [100500, 125000, 125000, 175000, 150000, 155000, 145000]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
DISPLAY myArray [ | ||
j + 2 * (k - j) / 2 | ||
]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* formatterSettingsOverride */ | ||
/* { "AblFormatter.arrayAccessFormatting": true}*/ | ||
|
||
DISPLAY myArray[j + 2 * (k - j) / 2]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { SyntaxNode } from "web-tree-sitter"; | ||
import { IFormatter } from "../../formatterFramework/IFormatter"; | ||
import { SyntaxNodeType } from "../../../model/SyntaxNodeType"; | ||
import { CodeEdit } from "../../model/CodeEdit"; | ||
import { FullText } from "../../model/FullText"; | ||
import { FormatterHelper } from "../../formatterFramework/FormatterHelper"; | ||
import { AFormatter } from "../AFormatter"; | ||
import { RegisterFormatter } from "../../formatterFramework/formatterDecorator"; | ||
import { IConfigurationManager } from "../../../utils/IConfigurationManager"; | ||
import { ArrayAccessSettings } from "./ArrayAccessSettings"; | ||
|
||
@RegisterFormatter | ||
export class ArrayAccessFormatter extends AFormatter implements IFormatter { | ||
public static readonly formatterLabel = "arrayAccessFormatting"; | ||
private readonly settings: ArrayAccessSettings; | ||
private formattingArrayLiteral: boolean = false; | ||
|
||
public constructor(configurationManager: IConfigurationManager) { | ||
super(configurationManager); | ||
this.settings = new ArrayAccessSettings(configurationManager); | ||
} | ||
|
||
match(node: Readonly<SyntaxNode>): boolean { | ||
if ( | ||
node.type === SyntaxNodeType.ArrayAccess || | ||
node.type === SyntaxNodeType.ArrayLiteral | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
parse( | ||
node: Readonly<SyntaxNode>, | ||
fullText: Readonly<FullText> | ||
): CodeEdit | CodeEdit[] | undefined { | ||
this.formattingArrayLiteral = node.type === SyntaxNodeType.ArrayLiteral; | ||
const oldText = FormatterHelper.getCurrentText(node, fullText); | ||
const text = this.collectString(node, fullText); | ||
return this.getCodeEdit(node, oldText, text, fullText); | ||
} | ||
|
||
private collectString( | ||
node: SyntaxNode, | ||
fullText: Readonly<FullText> | ||
): string { | ||
let resultString = ""; | ||
node.children.forEach((child) => { | ||
resultString = resultString.concat(this.getString(child, fullText)); | ||
}); | ||
return resultString; | ||
} | ||
|
||
private getString(node: SyntaxNode, fullText: Readonly<FullText>): string { | ||
let newString = ""; | ||
if (node.type === SyntaxNodeType.LeftBracket) { | ||
newString = FormatterHelper.getCurrentText(node, fullText).trim(); | ||
if (this.formattingArrayLiteral) { | ||
newString = " " + newString; | ||
} | ||
} else if ( | ||
node.type === SyntaxNodeType.RightBracket || | ||
(node.previousSibling !== null && | ||
node.previousSibling.type === SyntaxNodeType.LeftBracket) | ||
) { | ||
newString = FormatterHelper.getCurrentText( | ||
node, | ||
fullText | ||
).trimStart(); | ||
} else { | ||
if (this.formattingArrayLiteral) { | ||
newString = FormatterHelper.getCurrentText( | ||
node, | ||
fullText | ||
).trim(); | ||
if ( | ||
node.type === SyntaxNodeType.CommaKeyword && | ||
this.settings.addSpaceAfterComma() | ||
) { | ||
newString += " "; | ||
} | ||
} else { | ||
newString = FormatterHelper.getCurrentText(node, fullText); | ||
} | ||
} | ||
return newString; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ASettings } from "../ASettings"; | ||
|
||
export class ArrayAccessSettings extends ASettings { | ||
//empty block settings | ||
public ArrayAccessFormatting() { | ||
return !!this.configurationManager.get("arrayAccessFormatting"); | ||
} | ||
public addSpaceAfterComma() { | ||
return ( | ||
this.configurationManager.get( | ||
"arrayAccessFormattingAddSpaceAfterComma" | ||
) === "Yes" | ||
); | ||
} | ||
} |