forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompletionUtils.java
More file actions
99 lines (88 loc) · 2.87 KB
/
Copy pathCompletionUtils.java
File metadata and controls
99 lines (88 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.utils;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
/**
* Utility class for completions.
*/
public class CompletionUtils {
private CompletionUtils() {
}
/**
* Normalize line endings in a string to a specific line delimiter.
*
* @param text the input text.
* @param lineDelimiter the line delimiter to use.
* @return the normalized text.
*/
public static String normalizeLineEndings(String text, String lineDelimiter) {
if (text == null || text.isEmpty()) {
return text;
}
String delimiter = lineDelimiter != null ? lineDelimiter : System.lineSeparator();
String normalized = text.replace("\r\n", "\n").replace("\r", "\n");
if ("\n".equals(delimiter)) {
return normalized;
}
return normalized.replace("\n", delimiter);
}
/**
* Resolve the line delimiter for a document near the given offset.
*
* @param document the document to inspect.
* @param offset the offset used to determine the line.
* @return the resolved line delimiter.
*/
public static String getDocumentLineDelimiter(IDocument document, int offset) {
if (document == null) {
return System.lineSeparator();
}
try {
int safeOffset = Math.max(0, Math.min(offset, document.getLength()));
int line = document.getLineOfOffset(safeOffset);
String delimiter = document.getLineDelimiter(line);
if (delimiter != null) {
return delimiter;
}
} catch (BadLocationException e) {
// fall through to fallback delimiter
}
String[] legalDelimiters = document.getLegalLineDelimiters();
if (legalDelimiters != null && legalDelimiters.length > 0) {
return legalDelimiters[0];
}
return System.lineSeparator();
}
/**
* Replace all leading tabs for each line with spaces.
*
* @param input the input string.
* @param tabSize the tab size.
* @return the string with tabs replaced by spaces.
*/
public static String replaceTabsWithSpaces(String input, int tabSize) {
String[] lines = input.split("\n");
StringBuilder result = new StringBuilder();
for (String line : lines) {
result.append(replaceLeadingTabs(line, tabSize)).append("\n");
}
// Remove the last newline character
if (result.length() > 0) {
result.setLength(result.length() - 1);
}
return result.toString();
}
private static String replaceLeadingTabs(String line, int tabSize) {
int tabCount = countLeadingTabs(line);
String spaces = " ".repeat(tabSize).repeat(tabCount);
return spaces + line.substring(tabCount);
}
private static int countLeadingTabs(String line) {
int count = 0;
while (count < line.length() && line.charAt(count) == '\t') {
count++;
}
return count;
}
}