Skip to content

Commit 170d9bc

Browse files
authored
Merge pull request #134 from yury-mishchenko/main
Preserve file encoding and line endings in eclipse-coder::replaceFileContent/deleteLinesInFile
2 parents 5f1f861 + 5731cf9 commit 170d9bc

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

plugins/com.github.gradusnikov.eclipse.plugin.assistai.main/src/com/github/gradusnikov/eclipse/assistai/mcp/services/CodeEditingService.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,9 +2209,7 @@ public String replaceFileContent(String projectName, String filePath, String con
22092209
backupFile(file);
22102210

22112211
// Replace the file content
2212-
byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
2213-
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
2214-
file.setContents(inputStream, IResource.FORCE, null);
2212+
file.setContents(ResourceUtilities.toFileContent(file, content), IResource.FORCE, null);
22152213

22162214
// Refresh the file
22172215
file.refreshLocal(IResource.DEPTH_ZERO, null);
@@ -2282,38 +2280,31 @@ public String deleteLinesInFile(String projectName, String filePath, int startLi
22822280
backupFile(file);
22832281

22842282
// Read the file content
2285-
String fileContent = new String(file.getContents().readAllBytes(), StandardCharsets.UTF_8);
2286-
String[] lines = fileContent.split("\r?\n", -1);
2283+
var lines = ResourceUtilities.readFileLinesWithTerminators(file);
22872284

22882285
// Validate line numbers
2289-
if (startLine > lines.length)
2286+
if (startLine > lines.size())
22902287
{
2291-
throw new IllegalArgumentException("Error: Start line " + startLine + " is beyond the file length (" + lines.length + " lines).");
2288+
throw new IllegalArgumentException("Error: Start line " + startLine + " is beyond the file length (" + lines.size() + " lines).");
22922289
}
2293-
if (endLine > lines.length)
2290+
if (endLine > lines.size())
22942291
{
2295-
throw new IllegalArgumentException("Error: End line " + endLine + " is beyond the file length (" + lines.length + " lines).");
2292+
throw new IllegalArgumentException("Error: End line " + endLine + " is beyond the file length (" + lines.size() + " lines).");
22962293
}
22972294

22982295
// Build new content without the deleted lines
22992296
StringBuilder newContent = new StringBuilder();
2300-
for (int i = 0; i < lines.length; i++)
2297+
for (int i = 0; i < lines.size(); i++)
23012298
{
23022299
int lineNum = i + 1; // Convert to 1-based
23032300
if (lineNum < startLine || lineNum > endLine)
23042301
{
2305-
newContent.append(lines[i]);
2306-
if (i < lines.length - 1)
2307-
{
2308-
newContent.append("\n");
2309-
}
2302+
newContent.append(lines.get(i));
23102303
}
23112304
}
23122305

23132306
// Write the new content back to the file
2314-
byte[] bytes = newContent.toString().getBytes(StandardCharsets.UTF_8);
2315-
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
2316-
file.setContents(inputStream, IResource.FORCE, null);
2307+
file.setContents(ResourceUtilities.toFileContent(file, newContent.toString()), IResource.FORCE, null);
23172308

23182309
// Refresh the file
23192310
file.refreshLocal(IResource.DEPTH_ZERO, null);

plugins/com.github.gradusnikov.eclipse.plugin.assistai.main/src/com/github/gradusnikov/eclipse/assistai/tools/ResourceUtilities.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.io.InputStreamReader;
8+
import java.io.UnsupportedEncodingException;
89
import java.nio.charset.Charset;
910
import java.nio.file.Paths;
1011
import java.util.ArrayList;
@@ -16,6 +17,7 @@
1617
import java.io.BufferedInputStream;
1718
import java.io.ByteArrayInputStream;
1819
import org.eclipse.core.runtime.Platform;
20+
import org.eclipse.core.runtime.Status;
1921
import org.eclipse.core.runtime.content.IContentType;
2022
import org.eclipse.core.runtime.content.IContentTypeManager;
2123
import org.eclipse.core.resources.IContainer;
@@ -130,6 +132,17 @@ public static String readFileContent(IFile file) throws IOException, CoreExcepti
130132
}
131133
}
132134

135+
public static InputStream toFileContent(IFile file, String text) throws CoreException
136+
{
137+
Objects.requireNonNull(file);
138+
139+
try {
140+
return new ByteArrayInputStream(text == null ? new byte[0]: text.getBytes(file.getCharset()));
141+
} catch (UnsupportedEncodingException e) {
142+
throw new CoreException(Status.error("Unknown charset", e));
143+
}
144+
}
145+
133146
public static String getSuggestedFileName(String lang, String codeBlock)
134147
{
135148
// Fall back to the original implementation if parsing fails or for other languages

0 commit comments

Comments
 (0)