From f798869af3f7ae018bb3c64a3029a5be1740666b Mon Sep 17 00:00:00 2001 From: Wilian Gabriel Date: Tue, 15 Sep 2020 08:31:02 -0300 Subject: [PATCH 1/9] Fixing panic when get endOfPreviousLine and broken with out of range [-1] --- text/file.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/text/file.go b/text/file.go index 3604840..9af8610 100644 --- a/text/file.go +++ b/text/file.go @@ -114,9 +114,13 @@ func (textfile TextFile) FindLineAndColumn(findingIndex int) (line, column int) func (textfile TextFile) ExtractSample(findingIndex int) string { lineIndex := binarySearch(findingIndex, textfile.newlineEndingIndexes) + endOfPreviousLineIndex := 0 + if lineIndex >= 0 { + endOfPreviousLineIndex = lineIndex - 1 + } if lineIndex < len(textfile.newlineEndingIndexes) { - endOfPreviousLine := textfile.newlineEndingIndexes[lineIndex-1] + endOfPreviousLine := textfile.newlineEndingIndexes[endOfPreviousLineIndex] endOfCurrentLine := textfile.newlineEndingIndexes[lineIndex] lineContent := textfile.RawString[endOfPreviousLine+1 : endOfCurrentLine] From e5c5243414b2d7fa8455ded49d45226f71023ff3 Mon Sep 17 00:00:00 2001 From: Wilian Gabriel Date: Tue, 15 Sep 2020 08:33:18 -0300 Subject: [PATCH 2/9] Fixing panic when get endOfPreviousLine and broken with out of range [-1] --- text/file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/file.go b/text/file.go index 9af8610..8d17f40 100644 --- a/text/file.go +++ b/text/file.go @@ -115,7 +115,7 @@ func (textfile TextFile) FindLineAndColumn(findingIndex int) (line, column int) func (textfile TextFile) ExtractSample(findingIndex int) string { lineIndex := binarySearch(findingIndex, textfile.newlineEndingIndexes) endOfPreviousLineIndex := 0 - if lineIndex >= 0 { + if lineIndex > 0 { endOfPreviousLineIndex = lineIndex - 1 } From b76c6bb6644a81b31b13cf2d4726c480e3fe7f56 Mon Sep 17 00:00:00 2001 From: Wilian Gabriel Date: Tue, 15 Sep 2020 08:41:37 -0300 Subject: [PATCH 3/9] Fixing panic when get endOfPreviousLine and broken with out of range [-1] --- text/file.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/text/file.go b/text/file.go index 8d17f40..bfbd807 100644 --- a/text/file.go +++ b/text/file.go @@ -114,16 +114,11 @@ func (textfile TextFile) FindLineAndColumn(findingIndex int) (line, column int) func (textfile TextFile) ExtractSample(findingIndex int) string { lineIndex := binarySearch(findingIndex, textfile.newlineEndingIndexes) - endOfPreviousLineIndex := 0 - if lineIndex > 0 { - endOfPreviousLineIndex = lineIndex - 1 - } if lineIndex < len(textfile.newlineEndingIndexes) { - endOfPreviousLine := textfile.newlineEndingIndexes[endOfPreviousLineIndex] - endOfCurrentLine := textfile.newlineEndingIndexes[lineIndex] + index := textfile.newlineEndingIndexes[lineIndex] - lineContent := textfile.RawString[endOfPreviousLine+1 : endOfCurrentLine] + lineContent := textfile.RawString[index - 1 : index] return strings.TrimSpace(lineContent) } From ed9e6b20c61c1992d0dbe28c07eff0fcd8dc8319 Mon Sep 17 00:00:00 2001 From: Wilian Gabriel Date: Tue, 15 Sep 2020 08:55:02 -0300 Subject: [PATCH 4/9] Fixing panic when get endOfPreviousLine and broken with out of range [-1] --- text/file.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/text/file.go b/text/file.go index bfbd807..fa8df6a 100644 --- a/text/file.go +++ b/text/file.go @@ -115,10 +115,11 @@ func (textfile TextFile) FindLineAndColumn(findingIndex int) (line, column int) func (textfile TextFile) ExtractSample(findingIndex int) string { lineIndex := binarySearch(findingIndex, textfile.newlineEndingIndexes) - if lineIndex < len(textfile.newlineEndingIndexes) { - index := textfile.newlineEndingIndexes[lineIndex] + if lineIndex < len(textfile.newlineEndingIndexes) && lineIndex > 0 { + endOfPreviousLine := textfile.newlineEndingIndexes[lineIndex-1] + endOfCurrentLine := textfile.newlineEndingIndexes[lineIndex] - lineContent := textfile.RawString[index - 1 : index] + lineContent := textfile.RawString[endOfPreviousLine+1 : endOfCurrentLine] return strings.TrimSpace(lineContent) } From 1cf97d6d7aa911e4417038594d4407b1b9e169fc Mon Sep 17 00:00:00 2001 From: Wilian Gabriel Date: Tue, 15 Sep 2020 09:22:07 -0300 Subject: [PATCH 5/9] Adding method to get multi textFile --- text/file.go | 27 +++++++++++++++++++++++++++ text/file_test.go | 11 +++++++++++ 2 files changed, 38 insertions(+) diff --git a/text/file.go b/text/file.go index fa8df6a..834a1ae 100644 --- a/text/file.go +++ b/text/file.go @@ -157,6 +157,33 @@ func LoadDirIntoSingleUnit(path string, extensionsAccept []string) (TextUnit, er return unit, err } +// The Param extensionAccept is an filter to check if you need get textUnit for file with this extesion +// Example: []string{".java"} +// If an item of slice contains is equal the "**" it's will accept all extensions +// Example: []string{"**"} +func LoadDirIntoMultiUnit(path string, maxFilesPerTextUnit int, extensionsAccept []string) ([]TextUnit, error) { + units := []TextUnit{ + TextUnit{}, + } + lastIndexToAdd := 0 + err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { + if err != nil || info.IsDir() { + return err + } + textFile, err := validateAndGetTextFileByPath(path, extensionsAccept) + if err != nil || textFile == nil { + return err + } + units[lastIndexToAdd].Files = append(units[lastIndexToAdd].Files, *textFile) + if len(units[lastIndexToAdd].Files) >= maxFilesPerTextUnit { + units = append(units, TextUnit{}) + lastIndexToAdd++ + } + return nil + }) + return units, err +} + func validateAndGetTextFileByPath(path string, extensionsAccept []string) (*TextFile, error) { if checkIfEnableExtension(path, extensionsAccept) { file, err := getTextFileByPath(path) diff --git a/text/file_test.go b/text/file_test.go index 28f51c3..d3da054 100644 --- a/text/file_test.go +++ b/text/file_test.go @@ -185,6 +185,17 @@ func TestTextFiles_GetAllFilesUnits(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 9, len(textUnit.Files)) }) + t.Run("Should return multi unit with 4 textFiles and max of 3 files per textFile when get any files", func(t *testing.T) { + path := "./samples" + path, err := filepath.Abs(path) + assert.NoError(t, err) + textUnit, err := LoadDirIntoMultiUnit(path, 3, []string{"**"}) + assert.NoError(t, err) + assert.Equal(t, 4, len(textUnit)) + for _, item := range textUnit { + assert.LessOrEqual(t, len(item.Files), 3) + } + }) t.Run("Should return unit with tree files when get go files", func(t *testing.T) { path := "./samples" path, err := filepath.Abs(path) From c4691976b0d991d39e0cc820eefb3a3366d7053b Mon Sep 17 00:00:00 2001 From: Wilian Gabriel Date: Tue, 15 Sep 2020 10:13:35 -0300 Subject: [PATCH 6/9] Fixing panic when get endOfPreviousLine and broken with out of range [-1] --- text/file.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/text/file.go b/text/file.go index 834a1ae..29a88a7 100644 --- a/text/file.go +++ b/text/file.go @@ -116,10 +116,13 @@ func (textfile TextFile) ExtractSample(findingIndex int) string { lineIndex := binarySearch(findingIndex, textfile.newlineEndingIndexes) if lineIndex < len(textfile.newlineEndingIndexes) && lineIndex > 0 { - endOfPreviousLine := textfile.newlineEndingIndexes[lineIndex-1] + endOfPreviousLine := textfile.newlineEndingIndexes[lineIndex] + if lineIndex > 0 { + endOfPreviousLine = textfile.newlineEndingIndexes[lineIndex - 1] + 1 + } endOfCurrentLine := textfile.newlineEndingIndexes[lineIndex] - lineContent := textfile.RawString[endOfPreviousLine+1 : endOfCurrentLine] + lineContent := textfile.RawString[endOfPreviousLine : endOfCurrentLine] return strings.TrimSpace(lineContent) } From 17c438eaed9de66635851178efbaa388d459312f Mon Sep 17 00:00:00 2001 From: Wilian Gabriel Date: Tue, 15 Sep 2020 10:14:07 -0300 Subject: [PATCH 7/9] Fixing panic when get endOfPreviousLine and broken with out of range [-1] --- text/file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/file.go b/text/file.go index 29a88a7..209d277 100644 --- a/text/file.go +++ b/text/file.go @@ -115,7 +115,7 @@ func (textfile TextFile) FindLineAndColumn(findingIndex int) (line, column int) func (textfile TextFile) ExtractSample(findingIndex int) string { lineIndex := binarySearch(findingIndex, textfile.newlineEndingIndexes) - if lineIndex < len(textfile.newlineEndingIndexes) && lineIndex > 0 { + if lineIndex < len(textfile.newlineEndingIndexes) { endOfPreviousLine := textfile.newlineEndingIndexes[lineIndex] if lineIndex > 0 { endOfPreviousLine = textfile.newlineEndingIndexes[lineIndex - 1] + 1 From 71958ab761177897cf16d145b6bd14446d402b2d Mon Sep 17 00:00:00 2001 From: Wilian Gabriel Date: Tue, 15 Sep 2020 10:21:43 -0300 Subject: [PATCH 8/9] Fixing panic when get endOfPreviousLine and broken with out of range [-1] --- text/file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/file.go b/text/file.go index 209d277..a5c7bfa 100644 --- a/text/file.go +++ b/text/file.go @@ -116,7 +116,7 @@ func (textfile TextFile) ExtractSample(findingIndex int) string { lineIndex := binarySearch(findingIndex, textfile.newlineEndingIndexes) if lineIndex < len(textfile.newlineEndingIndexes) { - endOfPreviousLine := textfile.newlineEndingIndexes[lineIndex] + endOfPreviousLine := 0 if lineIndex > 0 { endOfPreviousLine = textfile.newlineEndingIndexes[lineIndex - 1] + 1 } From 3e90fc157d890e0ae8542c390c1252f3036bd319 Mon Sep 17 00:00:00 2001 From: Wilian Gabriel Date: Tue, 15 Sep 2020 13:24:09 -0300 Subject: [PATCH 9/9] Fixing column negative --- text/file.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/text/file.go b/text/file.go index a5c7bfa..7804ea2 100644 --- a/text/file.go +++ b/text/file.go @@ -106,7 +106,11 @@ func (textfile TextFile) FindLineAndColumn(findingIndex int) (line, column int) // now we access the textual index in the slice to ge the column endOfCurrentLineInTheFile := textfile.newlineEndingIndexes[endOfCurrentLine] - column = (findingIndex - 1) - endOfCurrentLineInTheFile + if findingIndex == 0 { + column = endOfCurrentLineInTheFile + } else { + column = (findingIndex - 1) - endOfCurrentLineInTheFile + } } return