|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2016 Avaloq Group AG and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Avaloq Group AG - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | + |
| 12 | +package com.avaloq.tools.ddk.xtext.check.generator.quickfix; |
| 13 | + |
| 14 | +import org.eclipse.xtend2.lib.StringConcatenationClient; |
| 15 | +import org.eclipse.xtext.Grammar; |
| 16 | +import org.eclipse.xtext.GrammarUtil; |
| 17 | +import org.eclipse.xtext.xtext.generator.AbstractXtextGeneratorFragment; |
| 18 | +import org.eclipse.xtext.xtext.generator.XtextGeneratorNaming; |
| 19 | +import org.eclipse.xtext.xtext.generator.model.FileAccessFactory; |
| 20 | +import org.eclipse.xtext.xtext.generator.model.TypeReference; |
| 21 | + |
| 22 | +import com.google.inject.Inject; |
| 23 | + |
| 24 | + |
| 25 | +@SuppressWarnings("nls") |
| 26 | +public class CheckQuickfixProviderFragment2 extends AbstractXtextGeneratorFragment { |
| 27 | + |
| 28 | + private static final String UI_PLUGIN = "com.avaloq.tools.ddk.check.runtime.ui"; |
| 29 | + |
| 30 | + @Inject |
| 31 | + private XtextGeneratorNaming xtextGeneratorNaming; |
| 32 | + |
| 33 | + @Inject |
| 34 | + private FileAccessFactory fileAccessFactory; |
| 35 | + |
| 36 | + protected TypeReference getQuickfixProviderClass(final Grammar g) { |
| 37 | + return new TypeReference( |
| 38 | + xtextGeneratorNaming.getEclipsePluginBasePackage(g) + ".quickfix." + GrammarUtil.getSimpleName(g) + "QuickfixProvider"); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void generate() { |
| 43 | + if (getProjectConfig().getEclipsePlugin().getManifest() != null) { |
| 44 | + getProjectConfig().getEclipsePlugin().getManifest().getRequiredBundles().add(UI_PLUGIN); |
| 45 | + } |
| 46 | + |
| 47 | + if (getProjectConfig().getEclipsePlugin().getSrc() != null) { |
| 48 | + generateQuickfixProvider(); |
| 49 | + } |
| 50 | + |
| 51 | + if (getProjectConfig().getEclipsePlugin().getPluginXml() != null) { |
| 52 | + addRegistrationToPluginXml(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + protected void generateQuickfixProvider() { |
| 57 | + final StringConcatenationClient content = new StringConcatenationClient() { |
| 58 | + @Override |
| 59 | + protected void appendTo(final TargetStringConcatenation builder) { |
| 60 | + builder.append(""" |
| 61 | + public class %s extends DefaultCheckQuickfixProvider { |
| 62 | +
|
| 63 | + // @Fix(MyJavaValidator.INVALID_NAME) |
| 64 | + // public void capitalizeName(final Issue issue, IssueResolutionAcceptor acceptor) { |
| 65 | + // acceptor.accept(issue, "Capitalize name", "Capitalize the name.", "upcase.png", new IModification() { |
| 66 | + // public void apply(IModificationContext context) throws BadLocationException { |
| 67 | + // IXtextDocument xtextDocument = context.getXtextDocument(); |
| 68 | + // String firstLetter = xtextDocument.get(issue.getOffset(), 1); |
| 69 | + // xtextDocument.replace(issue.getOffset(), 1, firstLetter.toUpperCase()); |
| 70 | + // } |
| 71 | + // }); |
| 72 | + // } |
| 73 | +
|
| 74 | + } |
| 75 | + """.formatted(getQuickfixProviderClass(getGrammar()).getSimpleName())); |
| 76 | + } |
| 77 | + }; |
| 78 | + fileAccessFactory.createJavaFile(getQuickfixProviderClass(getGrammar()), content).writeTo(getProjectConfig().getEclipsePlugin().getSrc()); |
| 79 | + } |
| 80 | + |
| 81 | + protected void addRegistrationToPluginXml() { |
| 82 | + final TypeReference executableExtensionFactory = xtextGeneratorNaming.getEclipsePluginExecutableExtensionFactory(getGrammar()); |
| 83 | + getProjectConfig().getEclipsePlugin().getPluginXml().getEntries().add(""" |
| 84 | + <!-- quickfix marker resolution generator --> |
| 85 | + <extension |
| 86 | + point="org.eclipse.ui.ide.markerResolution"> |
| 87 | + <markerResolutionGenerator |
| 88 | + class="%s:org.eclipse.xtext.ui.editor.quickfix.MarkerResolutionGenerator"> |
| 89 | + </markerResolutionGenerator> |
| 90 | + </extension> |
| 91 | + """.formatted(executableExtensionFactory)); |
| 92 | + } |
| 93 | +} |
0 commit comments