Skip to content

Commit

Permalink
Yaml<->Props conversion commands, tests, integration in VSCode, Eclipse
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Mar 28, 2024
1 parent e528454 commit aed2127
Show file tree
Hide file tree
Showing 22 changed files with 1,622 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
name="Convert .yaml to .properties">
</command>
</extension>
<!--
<extension
point="org.eclipse.ui.menus">
<menuContribution
Expand Down Expand Up @@ -90,6 +91,7 @@
</menuContribution>
</extension>
-->

<extension point="org.eclipse.ltk.core.refactoring.renameParticipants">
<renameParticipant class="org.springframework.ide.eclipse.boot.refactoring.typerename.TypeRenameParticipant" id="org.springframework.ide.eclipse.boot.refactoring.TypeRenameParticipant" name="Rename Companion Type Rename Participant">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
import org.eclipse.ui.handlers.HandlerUtil;
import org.springsource.ide.eclipse.commons.livexp.util.Log;
import org.eclipse.ltk.core.refactoring.RefactoringContext;

public class ConvertPropertiesToYamlHandler extends AbstractHandler {

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@
class="org.springframework.tooling.boot.ls.commands.AddStartersHandler"
commandId="spring.initializr.addStarters">
</handler>
<handler
class="org.springframework.tooling.boot.ls.commands.ConvertBootPropertiesHanlder$ConvertYamlToPropertiesHanlder"
commandId="org.springframework.tooling.boot.ls.properties.convert-yaml-to-props">
</handler>
<handler
class="org.springframework.tooling.boot.ls.commands.ConvertBootPropertiesHanlder$ConvertPropertiesToYamlHandler"
commandId="org.springframework.tooling.boot.ls.properties.convert-props-to-yaml">
</handler>
</extension>
<extension
point="org.eclipse.ui.commands">
Expand Down Expand Up @@ -329,6 +337,16 @@
typeId="org.eclipse.lsp4e.pathParameterType">
</commandParameter>
</command>
<command
description="Converts Spring Boot properties file from .properties format to .yaml format"
id="org.springframework.tooling.boot.ls.properties.convert-yaml-to-props"
name="Convert .yaml to .properties">
</command>
<command
description="Converts Spring Boot properties file from .yaml format to .properties format"
id="org.springframework.tooling.boot.ls.properties.convert-props-to-yaml"
name="Convert .properties to .yaml">
</command>
</extension>

<extension
Expand Down Expand Up @@ -765,6 +783,72 @@
</and>
</visibleWhen>
</command>
<command
commandId="org.springframework.tooling.boot.ls.properties.convert-props-to-yaml"
icon="icons/boot-icon.png">
<visibleWhen
checkEnabled="false">
<and>
<count
value="1">
</count>
<with
variable="activeMenuSelection">
<iterate
ifEmpty="false">
<adapt
type="org.eclipse.core.resources.IResource">
<and>
<test
property="org.springsource.ide.eclipse.boot.isBootResource">
</test>
<test
property="org.eclipse.core.resources.extension"
value="properties">
</test>
</and>
</adapt>
</iterate>
</with>
</and>
</visibleWhen>
</command>
<command
commandId="org.springframework.tooling.boot.ls.properties.convert-yaml-to-props"
icon="icons/boot-icon.png">
<visibleWhen
checkEnabled="false">
<and>
<count
value="1">
</count>
<with
variable="activeMenuSelection">
<iterate
ifEmpty="false">
<adapt
type="org.eclipse.core.resources.IResource">
<and>
<test
property="org.springsource.ide.eclipse.boot.isBootResource">
</test>
<or>
<test
property="org.eclipse.core.resources.extension"
value="yml">
</test>
<test
property="org.eclipse.core.resources.extension"
value="yaml">
</test>
</or>
</and>
</adapt>
</iterate>
</with>
</and>
</visibleWhen>
</command>
</menuContribution>
</extension>
<extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ public class Constants {
public static final String PREF_COMMON_PROPS_METADATA = "boot-java.common.properties-metadata";

public static final String PREF_MODULITH = "boot-java.modulith-project-tracking";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*******************************************************************************
* Copyright (c) 2024 Broadcom, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.tooling.boot.ls.commands;

import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.lsp4e.LanguageServers;
import org.eclipse.lsp4e.LanguageServersRegistry;
import org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.eclipse.ui.handlers.HandlerUtil;
import org.springframework.tooling.boot.ls.BootLanguageServerPlugin;

@SuppressWarnings("restriction")
public abstract class ConvertBootPropertiesHanlder extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IFile sourceFile = getSourceFile(event);
IProject project = sourceFile.getProject();
if (project != null) {

// No replace functionality for Eclipse due to issues with undoing the change
final boolean replace = false;

// ITextFileBuffer buffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(sourceFile.getFullPath(), LocationKind.IFILE);
// if (buffer != null && buffer.isDirty()) {
// try {
// buffer.commit(null, true);
// } catch (CoreException e) {
// BootLanguageServerPlugin.getDefault().getLog().log(e.getStatus());
// }
// }
//
// final boolean replace = BootLanguageServerPlugin.getDefault().getPreferenceStore().getBoolean(Constants.PREF_REPLACE_CONVERTED_PROPS_FILE);
//
// if (replace) {
// // Workaround: LSP4E LSPTextEdit doesn't work well with non opened files in the workspace in this particular case
// // Therefore, open the file in an editor thus changes are applied to the document.
// try {
// IDE.openEditor(UI.getActivePage(), sourceFile);
// } catch (PartInitException e) {
// BootLanguageServerPlugin.getDefault().getLog().error("", e);
// }
// }

IFile targetFile = getTargetFile(sourceFile, getTargetExtension());

LanguageServerDefinition def = LanguageServersRegistry.getInstance().getDefinition(BootLanguageServerPlugin.BOOT_LS_DEFINITION_ID);
Assert.isLegal(def != null, "No definition found for Boot Language Server");

ExecuteCommandParams commandParams = new ExecuteCommandParams();
commandParams.setCommand(getCommandId());
commandParams.setArguments(List.of(
sourceFile.getLocationURI().toASCIIString(),
targetFile.getLocationURI().toASCIIString(),
replace
));

LanguageServers.forProject(project).withPreferredServer(def).computeFirst(ls -> ls.getWorkspaceService().executeCommand(commandParams));

}

return null;
}

abstract protected String getTargetExtension();

abstract protected String getCommandId();

private IFile getTargetFile(IFile sourceFile, String ext) {
IProject project = sourceFile.getProject();
IPath eclipsePath = sourceFile.getProjectRelativePath();
IPath dir = eclipsePath.removeLastSegments(1);
String fileName = sourceFile.getName();
String fileNoExt = fileName.substring(0, fileName.length() - sourceFile.getFileExtension().length() - 1);
IFile target = project.getFile(dir.append("%s.%s".formatted(fileNoExt, ext)));
for (int i = 1; i < Integer.MAX_VALUE && target.exists(); i++) {
target = project.getFile(dir.append("%s-%d.%s".formatted(fileNoExt, i, ext)));
}
return target;
}

private IFile getSourceFile(ExecutionEvent event) {
ISelection selection = HandlerUtil.getActiveMenuSelection(event);
IStructuredSelection ss = null;
if (selection instanceof IStructuredSelection) {
ss = (IStructuredSelection) selection;
} else {
selection = HandlerUtil.getActiveMenuEditorInput(event);
if (selection instanceof IStructuredSelection) {
ss = (IStructuredSelection) selection;
}
}
if (ss!=null && !ss.isEmpty()) {
return asFile(ss.getFirstElement());
}
return null;
}

private IFile asFile(Object selectedElement) {
if (selectedElement instanceof IFile) {
return (IFile) selectedElement;
}
if (selectedElement instanceof IAdaptable) {
return ((IAdaptable) selectedElement).getAdapter(IFile.class);
}
return null;
}

public static class ConvertPropertiesToYamlHandler extends ConvertBootPropertiesHanlder {

@Override
protected String getTargetExtension() {
return "yml";
}

@Override
protected String getCommandId() {
return "sts/boot/props-to-yaml";
}

}

public static class ConvertYamlToPropertiesHanlder extends ConvertBootPropertiesHanlder {

@Override
protected String getTargetExtension() {
return "properties";
}

@Override
protected String getCommandId() {
return "sts/boot/yaml-to-props";
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void initializeDefaultPreferences() {
}));

preferenceStore.setDefault(Constants.PREF_MODULITH, true);

}

}
Loading

0 comments on commit aed2127

Please sign in to comment.