diff --git a/eclipse-extensions/org.springframework.ide.eclipse.boot.refactoring/src/org/springframework/ide/eclipse/boot/refactoring/ConvertYamlToPropertiesRefactoring.java b/eclipse-extensions/org.springframework.ide.eclipse.boot.refactoring/src/org/springframework/ide/eclipse/boot/refactoring/ConvertYamlToPropertiesRefactoring.java index 04a1aa7ed..8fbde189a 100644 --- a/eclipse-extensions/org.springframework.ide.eclipse.boot.refactoring/src/org/springframework/ide/eclipse/boot/refactoring/ConvertYamlToPropertiesRefactoring.java +++ b/eclipse-extensions/org.springframework.ide.eclipse.boot.refactoring/src/org/springframework/ide/eclipse/boot/refactoring/ConvertYamlToPropertiesRefactoring.java @@ -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; @@ -30,6 +29,7 @@ import org.eclipse.ltk.core.refactoring.RefactoringStatus; 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; @@ -86,80 +86,70 @@ public class ConvertYamlToPropertiesRefactoring extends Refactoring { 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 o = (Map) 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 o = (Map) 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(); + } + if (!hasComments && e instanceof CommentEvent ce) { + if (ce.getCommentType() == CommentType.BLANK_LINE) { + // document separator + } else { + hasComments = true; } } - return hasComments; - } catch (Throwable t) { - if (is != null) { - is.close(); - } - return true; } + return hasComments; } @Override diff --git a/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/composable/CompositeLanguageServerComponents.java b/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/composable/CompositeLanguageServerComponents.java index a57808474..793e25a90 100644 --- a/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/composable/CompositeLanguageServerComponents.java +++ b/headless-services/commons/commons-language-server/src/main/java/org/springframework/ide/vscode/commons/languageserver/composable/CompositeLanguageServerComponents.java @@ -184,14 +184,16 @@ public class CompositeLanguageServerComponents implements LanguageServerComponen @Override public List handle(DocumentSymbolParams params) { TextDocument doc = server.getTextDocumentService().getLatestSnapshot(params.getTextDocument().getUri()); - LanguageId language = doc.getLanguageId(); - List 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 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();