-
Notifications
You must be signed in to change notification settings - Fork 1
Provide indent-group feature #120
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 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 hidden or 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 |
|---|---|---|
|
|
@@ -44,6 +44,15 @@ type Fragment struct { | |
| Partitions []Partition | ||
| } | ||
|
|
||
| // partitionText contains selected source lines and their indentation group. | ||
| type partitionText struct { | ||
| // lines contains the source lines selected by one partition. | ||
| lines []string | ||
|
|
||
| // indentGroup identifies partitions whose common indentation is normalized together. | ||
| indentGroup string | ||
| } | ||
|
|
||
| // CreateDefaultFragment creates a whole-file fragment. | ||
| // | ||
| // Returns whole-file fragment. | ||
|
|
@@ -76,15 +85,12 @@ func (f Fragment) text(lines []string, separator string) (string, error) { | |
| if err != nil { | ||
| return "", err | ||
| } | ||
| var fragmentText []string | ||
| for _, partition := range partitionsTexts { | ||
| fragmentText = append(fragmentText, partition...) | ||
| } | ||
| indentation := indent.MaxCommonIndentation(fragmentText) | ||
| indentations := commonIndentations(partitionsTexts) | ||
|
|
||
| text := "" | ||
| for index, partitionText := range partitionsTexts { | ||
| cutIndentLines := indent.CutIndent(partitionText, indentation) | ||
| indentation := indentations[partitionText.indentGroup] | ||
| cutIndentLines := indent.CutIndent(partitionText.lines, indentation) | ||
|
|
||
| if index > 0 { | ||
| separatorIndentation := separatorIndent(cutIndentLines) | ||
|
|
@@ -105,17 +111,43 @@ func (f Fragment) text(lines []string, separator string) (string, error) { | |
| // Returns: | ||
| // [][]string - selected lines grouped by partition. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stale after the signature change: the function now returns |
||
| // error - when a partition cannot select its lines. | ||
| func (f Fragment) obtainPartitionTexts(lines []string) ([][]string, error) { | ||
| var partitionLines [][]string | ||
| func (f Fragment) obtainPartitionTexts(lines []string) ([]partitionText, error) { | ||
| var partitions []partitionText | ||
| for _, part := range f.Partitions { | ||
| partitionText, err := part.Select(lines) | ||
| selectedLines, err := part.Select(lines) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| partitionLines = append(partitionLines, partitionText) | ||
| partitions = append(partitions, partitionText{ | ||
| lines: selectedLines, | ||
| indentGroup: part.IndentGroup, | ||
| }) | ||
| } | ||
|
|
||
| return partitions, nil | ||
| } | ||
|
|
||
| // commonIndentations calculates common indentation for every partition group. | ||
| // | ||
| // Parameters: | ||
| // partitions - provides selected source lines in source order. | ||
| // | ||
| // Returns indentation width by group name. | ||
| func commonIndentations(partitions []partitionText) map[string]int { | ||
| groupLines := make(map[string][]string) | ||
| for _, partition := range partitions { | ||
| groupLines[partition.indentGroup] = append( | ||
| groupLines[partition.indentGroup], | ||
| partition.lines..., | ||
| ) | ||
| } | ||
|
|
||
| indentations := make(map[string]int, len(groupLines)) | ||
| for indentGroup, lines := range groupLines { | ||
| indentations[indentGroup] = indent.MaxCommonIndentation(lines) | ||
| } | ||
|
|
||
| return partitionLines, nil | ||
| return indentations | ||
| } | ||
|
|
||
| // separatorIndent returns the indentation to use before a partition separator. | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
The loop variable
partitionTexton line 91 now shadows thepartitionTexttype introduced at fragment.go:48, so the type name is unusable for the rest of the loop body and the reader meetspartitionText.indentGroup(a value) a few lines afterpartitionText{...}(a type). Renaming the variable topartition, ascommonIndentationsalready does, removes the collision.