Skip to content
This repository has been archived by the owner on Dec 4, 2022. It is now read-only.

Implement changes from issue #37 #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ Please make sure you have a backup (preferably in git) of your code before runni
`flutter-stylizer` in case it doesn't handle your code properly.


## [v0.1.17] - 2022-07-17

- A number of new optional classifications have been added to address
[issue #37](https://github.com/gmlewis/flutter-stylizer/issues/37).
- public-static-properties
- public-instance-properties
- public-override-properties
- private-static-properties
- private-instance-properties
- operators

## [v0.1.16] - 2022-06-28

- Fix [issue #31](https://github.com/gmlewis/flutter-stylizer/issues/31) for private vars.
Expand Down
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,43 @@ in the following manner (with a blank line separating these parts):
- (`named-constructors` in configuration)
* Any static (class) variables are listed next, in sorted order.
- (`public-static-variables` in configuration)
* If `public-static-properties` is (optionally) supplied, `get`ters and `set`ters
will be separated from `public-static-variables` and grouped together
(as pairs by name, first the `get` then the `set`).
Using this option will override the groupAndSortGetterMethods setting.
* Any instance variables are listed next, in sorted order.
- (`public-instance-variables` in configuration)
* If `public-instance-properties` is (optionally) supplied, `get`ters and `set`ters
will be separated from `public-instance-variables` and grouped together
(as pairs by name, first the `get` then the `set`).
Using this option will override the groupAndSortGetterMethods setting.
* Any `@override` variables are listed next, in sorted order.
- (`public-override-variables` in configuration)
* If `public-override-properties` is (optionally) supplied, `get`ters and `set`ters
will be separated from `public-override-variables` and grouped together
(as pairs by name, first the `get` then the `set`).
Using this option will override the groupAndSortGetterMethods setting.
* Any private static (class) variables are listed next, in sorted order.
- (`private-static-variables` in configuration)
* If `private-static-properties` is (optionally) supplied, `get`ters and `set`ters
will be separated from `private-static-variables` and grouped together
(as pairs by name, first the `get` then the `set`).
Using this option will override the groupAndSortGetterMethods setting.
* Any private instance variables are listed next, in sorted order.
- (`private-instance-variables` in configuration)
* If `private-instance-properties` is (optionally) supplied, `get`ters and `set`ters
will be separated from `private-instance-variables` and grouped together
(as pairs by name, first the `get` then the `set`).
Using this option will override the groupAndSortGetterMethods setting.
* Any `@override` methods are listed next, in sorted order.
- (`public-override-methods` in configuration)
* Any other methods are listed next in their original (unchanged) order.
(As of version `v0.0.19`, two new flags affect this section; see below.)
- (`public-other-methods` in configuration)
* If `private-other-methods` is (optionally) specified, these will be sorted
* If `private-other-methods` is (optionally) supplied, these will be sorted
separately from `public-other-methods`.
* If `operators` is (optionally) supplied, methods with the `operator` keyword
will be sorted and grouped separately from other methods above.
* The `build` method is listed last.
- (`build-method` in configuration)

Expand Down Expand Up @@ -182,13 +204,19 @@ memberOrdering:
public-constructor
named-constructors
public-static-variables
public-static-properties
public-instance-variables
public-instance-properties
public-override-variables
public-override-properties
private-static-variables
private-static-properties
private-instance-variables
private-instance-properties
public-override-methods
public-other-methods
private-other-methods
operators
build-method
processEnumsLikeClasses: false
sortClassesWithinFile: false
Expand Down Expand Up @@ -278,6 +306,17 @@ incorporate the fix into the repo.

## Release Notes

### v0.1.17

- A number of new optional classifications have been added to address
[issue #37](https://github.com/gmlewis/flutter-stylizer/issues/37).
- public-static-properties
- public-instance-properties
- public-override-properties
- private-static-properties
- private-instance-properties
- operators

### v0.1.16

- Fix [issue #31](https://github.com/gmlewis/flutter-stylizer/issues/31) for private vars.
Expand Down
73 changes: 55 additions & 18 deletions dart/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,31 @@ type Class struct {
openCurlyOffset int
closeCurlyOffset int
groupAndSortGetterMethods bool
separatePrivateMethods bool

hasPublicStaticProperties bool
hasPublicInstanceProperties bool
hasPublicOverrideProperties bool
hasPrivateStaticProperties bool
hasPrivateInstanceProperties bool
hasPrivateOtherMethods bool
hasOperators bool

theConstructor *Entity
namedConstructors []*Entity
staticVariables []*Entity
staticProperties []*Entity
instanceVariables []*Entity
instanceProperties []*Entity
overrideVariables []*Entity
overrideProperties []*Entity
staticPrivateVariables []*Entity
staticPrivateProperties []*Entity
privateVariables []*Entity
privateProperties []*Entity
overrideMethods []*Entity
otherAllOrPublicMethods []*Entity
otherPrivateMethods []*Entity
operators []*Entity
buildMethod *Entity
getterMethods []*Entity
}
Expand All @@ -60,7 +73,7 @@ type Class struct {
// groupAndSortGetterMethods determines how getter methods are processed.
func NewClass(editor *Editor, classType, className string,
openCurlyOffset, closeCurlyOffset int,
groupAndSortGetterMethods, separatePrivateMethods bool) *Class {
opts Options) *Class {
lessThanOffset := strings.Index(className, "<")
if lessThanOffset >= 0 { // Strip off <T>.
className = className[0:lessThanOffset]
Expand Down Expand Up @@ -108,8 +121,15 @@ func NewClass(editor *Editor, classType, className string,
openCurlyOffset: openCurlyOffset,
closeCurlyOffset: closeCurlyOffset,

groupAndSortGetterMethods: groupAndSortGetterMethods,
separatePrivateMethods: separatePrivateMethods,
groupAndSortGetterMethods: opts.GroupAndSortGetterMethods,

hasPublicStaticProperties: opts.hasPublicStaticProperties,
hasPublicInstanceProperties: opts.hasPublicInstanceProperties,
hasPublicOverrideProperties: opts.hasPublicOverrideProperties,
hasPrivateStaticProperties: opts.hasPrivateStaticProperties,
hasPrivateInstanceProperties: opts.hasPrivateInstanceProperties,
hasPrivateOtherMethods: opts.hasPrivateOtherMethods,
hasOperators: opts.hasOperators,
}
}

Expand Down Expand Up @@ -152,7 +172,7 @@ func (c *Class) FindFeatures() error {
return fmt.Errorf("identifyOthers: %w", err)
}

if c.e.Verbose {
if c.e.opts.Verbose {
for i, line := range c.lines {
c.e.logf("line #%v type=%v: %v", i+1, line.entityType, line.line)
}
Expand Down Expand Up @@ -513,7 +533,11 @@ func (c *Class) identifyOverrideMethodsAndVars() error {
}

if entity.entityType == OverrideVariable {
c.overrideVariables = append(c.overrideVariables, entity)
if c.hasPublicOverrideProperties && entity.isProperty() {
c.overrideProperties = append(c.overrideProperties, entity)
} else {
c.overrideVariables = append(c.overrideVariables, entity)
}
} else {
c.overrideMethods = append(c.overrideMethods, entity)
}
Expand Down Expand Up @@ -561,9 +585,20 @@ func (c *Class) identifyOthers() error {

switch entity.entityType {
case OtherMethod:
if c.separatePrivateMethods && entity.isPrivateMethod() {
switch {
case c.hasPublicStaticProperties && entity.isProperty() && !entity.private && entity.static:
c.staticProperties = append(c.staticProperties, entity)
case c.hasPrivateStaticProperties && entity.isProperty() && entity.private && entity.static:
c.staticPrivateProperties = append(c.staticPrivateProperties, entity)
case c.hasPublicInstanceProperties && entity.isProperty() && !entity.private && !entity.static:
c.instanceProperties = append(c.instanceProperties, entity)
case c.hasPrivateInstanceProperties && entity.isProperty() && entity.private && !entity.static:
c.privateProperties = append(c.privateProperties, entity)
case c.hasOperators && entity.isOperator():
c.operators = append(c.operators, entity)
case c.hasPrivateOtherMethods && entity.isPrivateMethod():
c.otherPrivateMethods = append(c.otherPrivateMethods, entity)
} else {
default:
c.otherAllOrPublicMethods = append(c.otherAllOrPublicMethods, entity)
}
case GetterMethod:
Expand All @@ -575,7 +610,11 @@ func (c *Class) identifyOthers() error {
case InstanceVariable:
c.instanceVariables = append(c.instanceVariables, entity)
case OverrideVariable:
c.overrideVariables = append(c.overrideVariables, entity)
if c.hasPublicOverrideProperties && entity.isProperty() {
c.overrideProperties = append(c.overrideProperties, entity)
} else {
c.overrideVariables = append(c.overrideVariables, entity)
}
case PrivateInstanceVariable:
c.privateVariables = append(c.privateVariables, entity)
case LeaveUnmodified:
Expand All @@ -597,27 +636,25 @@ func (c *Class) scanMethod(lineNum int) (*Entity, error) {
c.e.logf("scanMethod(line=#%v), sequence=%v, lineCount=%v, leadingText=%q", lineNum+1, sequence, lineCount, leadingText)

nameParts := strings.Split(leadingText, " ")
var staticKeyword bool
var privateVar bool
if len(nameParts) > 0 {
entity.name = nameParts[len(nameParts)-1]
if strings.HasPrefix(entity.name, "_") {
privateVar = true
entity.private = true
}
if nameParts[0] == "static" {
staticKeyword = true
entity.static = true
}
}

entity.entityType = InstanceVariable
switch {
case c.classType == "enum" && lineNum == 1:
entity.entityType = LeaveUnmodified
case privateVar && staticKeyword:
case entity.private && entity.static:
entity.entityType = StaticPrivateVariable
case staticKeyword:
case entity.static:
entity.entityType = StaticVariable
case privateVar:
case entity.private:
entity.entityType = PrivateInstanceVariable
}

Expand All @@ -639,8 +676,8 @@ func (c *Class) scanMethod(lineNum int) (*Entity, error) {
}
}

// Force getters to be methods.
if strings.Contains(leadingText, " get ") {
// Force getters and setters to be methods.
if strings.Contains(leadingText, " get ") || strings.Contains(leadingText, "set ") {
if c.groupAndSortGetterMethods {
entity.entityType = GetterMethod
} else {
Expand Down
19 changes: 6 additions & 13 deletions dart/class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,16 @@ func runParsePhase(t *testing.T, opts *Options, source string, wantClasses [][]E

testOpts := Options{MemberOrdering: defaultMemberOrdering}
if opts != nil {
testOpts.GroupAndSortGetterMethods = opts.GroupAndSortGetterMethods
testOpts.GroupAndSortVariableTypes = opts.GroupAndSortVariableTypes
testOpts.MemberOrdering = opts.MemberOrdering
testOpts.ProcessEnumsLikeClasses = opts.ProcessEnumsLikeClasses
testOpts.SortClassesWithinFile = opts.SortClassesWithinFile
testOpts.SortOtherMethods = opts.SortOtherMethods
testOpts.Debug = opts.Debug
testOpts.Verbose = opts.Verbose
testOpts = *opts
}

e, err := NewEditor(source, testOpts.ProcessEnumsLikeClasses, testOpts.Verbose)
e, err := NewEditor(source, testOpts)
if err != nil {
t.Fatalf("NewEditor: %v", err)
}

c := &Client{editor: e, opts: testOpts}
gotAll, err := e.GetClasses(testOpts.GroupAndSortGetterMethods, testOpts.SeparatePrivateMethods)
c := New(e, testOpts)
gotAll, err := e.GetClasses()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1132,7 +1125,7 @@ func TestIssue19_FactoryConstructorShouldNotBeDuplicated(t *testing.T) {
func TestFindFeatures_linux_mac(t *testing.T) {
bc, bcLineOffset, bcOCO, bcCCO := setupEditor(t, "class Class1 {", basicClasses)

uc := NewClass(bc, "class", "Class1", bcOCO, bcCCO, false, false)
uc := NewClass(bc, "class", "Class1", bcOCO, bcCCO, Options{})

want := []EntityType{
Unknown, // line #7: class Class1 {
Expand Down Expand Up @@ -1206,7 +1199,7 @@ func TestFindFeatures_linux_mac(t *testing.T) {
func TestFindFeatures_windoze(t *testing.T) {
wz, wzLineOffset, wzOCO, wzCCO := setupEditor(t, "class Class1 {", bcWindoze)

wc := NewClass(wz, "class", "Class1", wzOCO, wzCCO, false, false)
wc := NewClass(wz, "class", "Class1", wzOCO, wzCCO, Options{})

want := []EntityType{
Unknown, // line #7: class Class1 {
Expand Down
Loading