Skip to content

Commit

Permalink
Yaml->Props file rename issue on Win fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Mar 26, 2024
1 parent 6422fdc commit e528454
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
package org.springframework.ide.eclipse.boot.refactoring;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import java.util.Properties;
Expand All @@ -30,6 +29,7 @@
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.ltk.core.refactoring.resource.RenameResourceChange;
import org.eclipse.text.edits.ReplaceEdit;
import org.springsource.ide.eclipse.commons.frameworks.core.util.IOUtil;
import org.springsource.ide.eclipse.commons.livexp.util.ExceptionUtil;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
Expand Down Expand Up @@ -86,80 +86,70 @@ private static IFile propsFileFor(IFile yamlFile) {
public RefactoringStatus checkFinalConditions(IProgressMonitor pm)
throws CoreException, OperationCanceledException {
RefactoringStatus status = new RefactoringStatus();
String yamlContent = null;
try {
if (hasComments(yamlFile)) {
status.merge(RefactoringStatus.createWarningStatus("The yaml file has comments, which will be lost in the refactoring!"));
}
yamlContent = IOUtil.toString(yamlFile.getContents());
} catch (Exception e) {
status.merge(RefactoringStatus.create(ExceptionUtil.status(e, "Problems reading file: "+yamlFile.getFullPath())));
return status;
}

try (InputStream content = yamlFile.getContents()) {
for (Object d : new Yaml().loadAll(yamlFile.getContents())) {
if (d instanceof Map) {
// Add doc divider if not empty
@SuppressWarnings("unchecked")
Map<String, ?> o = (Map<String, ?>) d;
try {
YamlToPropertiesConverter converter = new YamlToPropertiesConverter(o);
Properties props = converter.getProperties();
StringWriter write = new StringWriter();
props.store(write, null);
write.flush();
write.close();
if (!propsContent.isEmpty()) {
propsContent.append("#---\n");
}
// Skip over the date header. Comments are not present but date header is.
if (write.getBuffer().charAt(0) == '#') {
int idx = write.getBuffer().indexOf("\n");
this.propsContent.append(idx >= 0 && idx < write.getBuffer().length() ? write.getBuffer().substring(idx + 1) : write.getBuffer().toString());
} else {
this.propsContent.append(write.getBuffer().toString());
}
status.merge(converter.getStatus());
} catch (IOException e) {
status.merge(RefactoringStatus.create(ExceptionUtil.status(e, "Problems writing to .properties file: "+propsFile.getFullPath())));
}
} else if (d == null) {
if (hasComments(yamlContent)) {
status.merge(RefactoringStatus.createWarningStatus("The yaml file has comments, which will be lost in the refactoring!"));
}

for (Object d : new Yaml().loadAll(new StringReader(yamlContent))) {
if (d instanceof Map) {
// Add doc divider if not empty
@SuppressWarnings("unchecked")
Map<String, ?> o = (Map<String, ?>) d;
try {
YamlToPropertiesConverter converter = new YamlToPropertiesConverter(o);
Properties props = converter.getProperties();
StringWriter write = new StringWriter();
props.store(write, null);
write.flush();
write.close();
if (!propsContent.isEmpty()) {
propsContent.append("#---\n");
}
// Skip over the date header. Comments are not present but date header is.
if (write.getBuffer().charAt(0) == '#') {
int idx = write.getBuffer().indexOf("\n");
this.propsContent.append(idx >= 0 && idx < write.getBuffer().length() ? write.getBuffer().substring(idx + 1) : write.getBuffer().toString());
} else {
this.propsContent.append(write.getBuffer().toString());
}
status.merge(converter.getStatus());
} catch (IOException e) {
status.merge(RefactoringStatus.create(ExceptionUtil.status(e, "Problems writing to .properties file: "+propsFile.getFullPath())));
}
} else if (d == null) {
if (!propsContent.isEmpty()) {
propsContent.append("#---\n");
}
}
} catch (Exception e) {
status.merge(RefactoringStatus.create(ExceptionUtil.status(e, "Problems parsing as a .yaml file: "+yamlFile.getFullPath())));
}
return status;
}

private boolean hasComments(IFile yamlFile) throws Exception {
InputStream is = null;
try {
is = yamlFile.getContents();
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setProcessComments(true);
boolean hasComments = false;
for (Event e : new Yaml(loaderOptions).parse(new InputStreamReader(is))) {
if (e instanceof StreamEndEvent) {
inputTextLen = e.getEndMark().getIndex();
}
if (!hasComments && e instanceof CommentEvent ce) {
if (ce.getCommentType() == CommentType.BLANK_LINE) {
// document separator
} else {
hasComments = true;
}
}
private boolean hasComments(String yamlContent) {
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setProcessComments(true);
boolean hasComments = false;
for (Event e : new Yaml(loaderOptions).parse(new StringReader(yamlContent))) {
if (e instanceof StreamEndEvent) {
inputTextLen = e.getEndMark().getIndex();
}
return hasComments;
} catch (Throwable t) {
if (is != null) {
is.close();
if (!hasComments && e instanceof CommentEvent ce) {
if (ce.getCommentType() == CommentType.BLANK_LINE) {
// document separator
} else {
hasComments = true;
}
}
return true;
}
return hasComments;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,16 @@ public List<InlayHint> handle(CancelChecker token, InlayHintParams params) {
@Override
public List<? extends WorkspaceSymbol> handle(DocumentSymbolParams params) {
TextDocument doc = server.getTextDocumentService().getLatestSnapshot(params.getTextDocument().getUri());
LanguageId language = doc.getLanguageId();
List<LanguageServerComponents> subComponents = componentsByLanguageId.get(language);
if (subComponents != null) {
return subComponents.stream()
.map(sc -> sc.getDocumentSymbolProvider())
.filter(ds -> ds.isPresent())
.flatMap(ds -> ds.get().handle(params).stream())
.collect(Collectors.toList());
if (doc != null) {
LanguageId language = doc.getLanguageId();
List<LanguageServerComponents> subComponents = componentsByLanguageId.get(language);
if (subComponents != null) {
return subComponents.stream()
.map(sc -> sc.getDocumentSymbolProvider())
.filter(ds -> ds.isPresent())
.flatMap(ds -> ds.get().handle(params).stream())
.collect(Collectors.toList());
}
}
//No applicable subEngine...
return Collections.emptyList();
Expand Down

0 comments on commit e528454

Please sign in to comment.