Skip to content

Commit

Permalink
Merge pull request #102 from de-jcup/feature-100-simple-formatter
Browse files Browse the repository at this point in the history
Implemented simple formatter #100
  • Loading branch information
de-jcup authored Oct 11, 2024
2 parents df8486e + 20179e4 commit ba938f0
Show file tree
Hide file tree
Showing 31 changed files with 1,056 additions and 211 deletions.
432 changes: 224 additions & 208 deletions jenkins-editor-other/testscripts/declarative-full/Jenkinsfile

Large diffs are not rendered by default.

42 changes: 41 additions & 1 deletion jenkins-editor-plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
id="jenkinseditor.preferences.keyword.editor"
label="editor">
</keyword>
<keyword
id="jenkinseditor.preferences.keyword.format"
label="format">
</keyword>
<keyword
id="jenkinseditor.preferences.keyword.formatter"
label="formatter">
</keyword>
</extension>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- CONSOLE -->
Expand Down Expand Up @@ -151,6 +159,12 @@
categoryId="jenkinseditor.commands.category"
id="jenkinseditor.editor.commands.fetchLastBuildLogs">
</command>
<command
name="Format"
description="Formats descriptive jenkins pipeline sourcecode"
categoryId="jenkinseditor.commands.category"
id="jenkinseditor.editor.commands.source.format">
</command>

</extension>

Expand Down Expand Up @@ -183,6 +197,10 @@
commandId="jenkinseditor.editor.commands.fetchLastBuildLogs"
class="de.jcup.jenkinseditor.handlers.RestoreBuildLogToConsoleHandler">
</handler>
<handler
commandId="jenkinseditor.editor.commands.source.format"
class="de.jcup.jenkinseditor.handlers.FormatJenkinsSourceHandler">
</handler>
</extension>

<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
Expand All @@ -207,6 +225,12 @@
commandId="jenkinseditor.editor.commands.quickoutline"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
</key>
<key
sequence="M1+SHIFT+F"
contextId="de.jcup.jenkinseditor.editors.JenkinsEditor.context"
commandId="jenkinseditor.editor.commands.source.format"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
</key>
</extension>

<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
Expand Down Expand Up @@ -244,7 +268,8 @@
value="de.jcup.jenkinseditor.JenkinsEditor"/>
</with>
</visibleWhen>
<command commandId="jenkinseditor.editor.commands.source.toggleComment">
<command commandId="jenkinseditor.editor.commands.source.format"></command>
<command commandId="jenkinseditor.editor.commands.source.toggleComment">
</command>
</menu>
</menuContribution>
Expand Down Expand Up @@ -356,6 +381,21 @@
id="jenkinseditor.preferences.keyword.tasks">
</keywordReference>
</page>
<page
id="jenkinseditor.eclipse.gradleeditor.preferences.JenkinsEditorFormatterPreferencePage"
class="de.jcup.jenkinseditor.preferences.JenkinsEditorFormatterPreferencePage"
name="Formatter"
category="jenkinseditor.eclipse.gradleeditor.preferences.JenkinsEditorEditorPreferencePage">
<keywordReference
id="jenkinseditor.preferences.keyword.jenkins">
</keywordReference>
<keywordReference
id="jenkinseditor.preferences.keyword.format">
</keywordReference>
<keywordReference
id="jenkinseditor.preferences.keyword.formatter">
</keywordReference>
</page>
</extension>
<extension
point="org.eclipse.core.runtime.preferences">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package de.jcup.jenkinseditor;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.texteditor.IDocumentProvider;
Expand Down Expand Up @@ -46,6 +47,8 @@ public class JenkinsEditor extends AbstractGroovyBasedEditor {
private static final JenkinsFileDocumentProvider SHARED_FILE_DOCUMENT_PROVIDER = new JenkinsFileDocumentProvider();

private PipelineConfigData pipelineConfigData = new PipelineConfigData();

private static final SimpleJenkinsDeclartivePipelineSourceFormatter sourceFormatter = new SimpleJenkinsDeclartivePipelineSourceFormatter();


@Override
Expand Down Expand Up @@ -113,7 +116,13 @@ protected String getEditorIconPath() {
protected String getEditorIconPathOnError() {
return "icons/jenkinseditor/jenkins-editor-with-error.png";
}

String getText() {
IDocument doc = getDocument();
if (doc == null) {
return "";
}
return doc.get();
}
/**
*
* @return replay data for this script, never <code>null</code>
Expand All @@ -122,4 +131,11 @@ public PipelineConfigData getReplayData() {
return pipelineConfigData;
}

public void formatSourceCode() {
String code = getText();
sourceFormatter.setIndent(JenkinsEditorPreferences.getInstance().getSourceFormatIndention());
String formatted = sourceFormatter.format(code);
getDocument().set(formatted);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2019 Albert Tregnaghi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
*
*/
package de.jcup.jenkinseditor.handlers;

import de.jcup.jenkinseditor.JenkinsEditor;

public class FormatJenkinsSourceHandler extends AbstractJenkinsCLIHandler {

@Override
protected void executeOnActiveJenkinsEditor(JenkinsEditor editor) {
editor.formatSourceCode();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package de.jcup.jenkinseditor.preferences;
/*
* Copyright 2024 Albert Tregnaghi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
*
*/

import static de.jcup.jenkinseditor.preferences.JenkinsEditorPreferenceConstants.*;

import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

public class JenkinsEditorFormatterPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

public JenkinsEditorFormatterPreferencePage() {
setPreferenceStore(JenkinsEditorPreferences.getInstance().getPreferenceStore());
}

@Override
public void init(IWorkbench workbench) {

}

@Override
protected void createFieldEditors() {
Composite parent = getFieldEditorParent();

IntegerFieldEditor indentEditor = new IntegerFieldEditor(P_SOURCE_FORMAT_INDENT.getId(),
"Indent / Tab replacement", parent);
indentEditor.setValidRange(2, 10);
addField(indentEditor);
indentEditor.getLabelControl(parent).setToolTipText(
"Amount of spaces used by source formatter to make indention");


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public enum JenkinsEditorPreferenceConstants implements PreferenceIdentifiable{
P_EDITOR_CODEASSIST_NO_PROPOSALS_FOR_GETTER_OR_SETTERS("codeAssistNoProposalsForGetterOrSetter"),
P_EDITOR_CODEASSIST_TOOLTIPS_ENABLED("codeAssistTooltipsEnabled"),

P_SOURCE_FORMAT_INDENT("sourceFormatIndent"),

P_JENKINS_URL("jenkinsURL"),

P_PATH_TO_JENKINS_CLI_JAR("pathToJenkinsCliJar"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public void initializeDefaultPreferences() {
store.setDefault(P_EDITOR_CODEASSIST_PROPOSALS_ENABLED.getId(), true);
store.setDefault(P_EDITOR_CODEASSIST_NO_PROPOSALS_FOR_GETTER_OR_SETTERS.getId(), true);
store.setDefault(P_EDITOR_CODEASSIST_TOOLTIPS_ENABLED.getId(), true);

store.setDefault(P_SOURCE_FORMAT_INDENT.getId(), 3);

store.setDefault(P_EDITOR_SHOW_ALSO_NON_STRICT_CODE_PROPOSALS.getId(), false);

Expand Down Expand Up @@ -81,7 +83,6 @@ public void initializeDefaultPreferences() {
preferences.setDefaultColor(COLOR_JENKINS_VARIABLES, JenkinsEditorColorConstants.DARK_BLUE);
preferences.setDefaultColor(COLOR_JAVA_LITERAL, JenkinsEditorColorConstants.KEYWORD_DEFAULT_PURPLE);


}

private JenkinsEditorPreferences getPreferences() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public boolean isCodeAssistTooltipsEnabled() {
public String getJenkinsURL() {
return getStringPreference(JenkinsEditorPreferenceConstants.P_JENKINS_URL);
}

public int getSourceFormatIndention() {
return getPreferenceStore().getInt(P_SOURCE_FORMAT_INDENT.getId());
}

@Override
public boolean isEditorAutoCreateEndBracketsEnabled() {
Expand Down
Loading

0 comments on commit ba938f0

Please sign in to comment.