-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
285 Implement ArrayAccessFormatter #285
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we have setting for having no space after comma? I think it should always have it.