-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improves suggestion text sanitization logic
- Loading branch information
Showing
2 changed files
with
58 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
55 changes: 55 additions & 0 deletions
55
plugin/src/software/aws/toolkits/eclipse/amazonq/util/SuggestionTextUtil.java
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,55 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
package software.aws.toolkits.eclipse.amazonq.util; | ||
|
||
public final class SuggestionTextUtil { | ||
|
||
private SuggestionTextUtil() { | ||
} | ||
|
||
public static String replaceSpacesWithTabs(String input, int tabSize) { | ||
StringBuilder result = new StringBuilder(); | ||
String[] lines = input.split("\\r?\\n"); | ||
|
||
for (int i = 0; i < lines.length; i++) { | ||
String line = lines[i]; | ||
int numSpaces = 0; | ||
StringBuilder newLine = new StringBuilder(); | ||
|
||
for (int j = 0; j < line.length(); j++) { | ||
char c = line.charAt(j); | ||
if (c == ' ') { | ||
numSpaces++; | ||
} else { | ||
newLine.append(getTabsForSpaces(numSpaces, tabSize)); | ||
newLine.append(c); | ||
numSpaces = 0; | ||
} | ||
} | ||
|
||
if (i < lines.length - 1) { | ||
newLine.append("\n"); | ||
} | ||
|
||
result.append(newLine); | ||
} | ||
|
||
return result.toString(); | ||
} | ||
|
||
private static String getTabsForSpaces(int numSpaces, int tabSize) { | ||
int numTabs = numSpaces / tabSize; | ||
StringBuilder tabs = new StringBuilder(); | ||
|
||
for (int i = 0; i < numTabs; i++) { | ||
tabs.append("\t"); | ||
} | ||
|
||
int remainingSpaces = numSpaces % tabSize; | ||
for (int i = 0; i < remainingSpaces; i++) { | ||
tabs.append(" "); | ||
} | ||
|
||
return tabs.toString(); | ||
} | ||
} |