Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add refresh project in-line shortcut to Gradle script editor #847

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions org.eclipse.buildship.ui/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" path="src/main/java-e48"/>
<classpathentry kind="output" path="bin"/>
</classpath>
10 changes: 10 additions & 0 deletions org.eclipse.buildship.ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ dependencies {
compile withEclipseBundle("org.eclipse.swt.${ECLIPSE_WS}.${ECLIPSE_OS}.${ECLIPSE_ARCH}")
}

// use java-e48 source dir for all Eclipse 4.8+ builds
if (eclipsebuild.Config.on(project).targetPlatform.eclipseVersion =~ /48|49|41[0-9]|4[2-9][0-9]|[5-9][0-9][0-9]?/) {
sourceSets {
main {
java {
srcDirs += 'java-e48'
}
}
}
}
1 change: 1 addition & 0 deletions org.eclipse.buildship.ui/build.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source.. = src/main/java/,\
src/main/java-e48,\
src/main/resources/
output.. = bin/
bin.includes = .,\
Expand Down
28 changes: 28 additions & 0 deletions org.eclipse.buildship.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,32 @@
</themeid>
</stylesheet>
</extension>

<!-- Contribute synchronization shortcut to Gradle editor -->
<extension
point="org.eclipse.ui.workbench.texteditor.codeMiningProviders">
<codeMiningProvider
class="org.eclipse.buildship.ui.internal.editor.GradleEditorCodeMiningProvider"
id="org.eclipse.buildship.ui.editor.codemining.synchronization">
<enabledWhen>
<with
variable="editorInput">
<adapt
type="org.eclipse.core.resources.IResource">
<and>
<test
forcePluginActivation="true"
property="org.eclipse.core.resources.projectNature"
value="org.eclipse.buildship.core.gradleprojectnature">
</test>
<test
forcePluginActivation="true"
property="org.eclipse.buildship.core.isGradleResource">
</test>
</and>
</adapt>
</with>
</enabledWhen>
</codeMiningProvider>
</extension>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2019 the original author or authors.
* 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
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.eclipse.buildship.ui.internal.editor;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.codemining.AbstractCodeMiningProvider;
import org.eclipse.jface.text.codemining.ICodeMining;

/**
* Contributes Gradle project synchronization to code mining.
*
* @author Donat Csikos
*/
public class GradleEditorCodeMiningProvider extends AbstractCodeMiningProvider {

@Override
public CompletableFuture<List<? extends ICodeMining>> provideCodeMinings(ITextViewer viewer, IProgressMonitor monitor) {
return CompletableFuture.supplyAsync(() -> {
try {
return Arrays.asList(new ProjectSynchronizerCodeMining(viewer.getDocument(), this));
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2019 the original author or authors.
* 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
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.eclipse.buildship.ui.internal.editor;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.codemining.ICodeMiningProvider;
import org.eclipse.jface.text.codemining.LineHeaderCodeMining;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

import org.eclipse.buildship.core.GradleCore;
import org.eclipse.buildship.core.internal.workspace.NewProjectHandler;
import org.eclipse.buildship.core.internal.workspace.SynchronizationJob;
import org.eclipse.buildship.ui.internal.PluginImage.ImageState;
import org.eclipse.buildship.ui.internal.PluginImages;

/**
* Adds synchronize action code mining.
*
* @author Donat Csikos
*/
public class ProjectSynchronizerCodeMining extends LineHeaderCodeMining {

private static final String REFRESH_LABEL = "Refresh Gradle build";

private final IDocument document;

public ProjectSynchronizerCodeMining(IDocument document, ICodeMiningProvider provider) throws BadLocationException {
super(0, document, provider);
this.document = document;
}

@Override
protected CompletableFuture<Void> doResolve(ITextViewer viewer, IProgressMonitor monitor) {
return CompletableFuture.runAsync(() -> {
super.setLabel(REFRESH_LABEL);
return;
});
}

@Override
public Point draw(GC gc, StyledText textWidget, Color color, int x, int y) {
int imageSize = textWidget.getLineHeight(); // rectangular image
Image image = getImage();
Rectangle imageBounds = image.getBounds();
gc.drawImage(image, 0, 0, imageBounds.width, imageBounds.height, x, y, imageSize, imageSize);
gc.drawText(REFRESH_LABEL, x + imageSize, y);
return new Point(imageSize + gc.textExtent(REFRESH_LABEL).x, imageSize);
}

private Image getImage() {
return PluginImages.REFRESH.withState(ImageState.DISABLED).getImage();
}

@Override
public Consumer<MouseEvent> getAction() {
return t -> executeSynchronization(t);
}

private void executeSynchronization(MouseEvent t) {
if (this.document != null) {
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
if (manager != null) {
ITextFileBuffer buffer = manager.getTextFileBuffer(this.document);
if (buffer != null) {
IPath path = buffer.getLocation();
if (path != null) {
IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
if (resource != null) {
IProject project = resource.getProject();
GradleCore.getWorkspace().getBuild(project).ifPresent(build -> new SynchronizationJob(NewProjectHandler.IMPORT_AND_MERGE, build).schedule());
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package org.eclipse.buildship.ui.internal.editor;

import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
Expand All @@ -34,11 +33,6 @@ public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceVie
return reconciler;
}

@Override
public IReconciler getReconciler(ISourceViewer sourceViewer) {
return null;
}

@Override
public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
return GradleEditorConstants.PARTITIONS;
Expand Down