diff --git a/plugins/org.eclipse.xtext.xtext.generator/META-INF/MANIFEST.MF b/plugins/org.eclipse.xtext.xtext.generator/META-INF/MANIFEST.MF index f140da1ed..93ba0ef94 100644 --- a/plugins/org.eclipse.xtext.xtext.generator/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.xtext.xtext.generator/META-INF/MANIFEST.MF @@ -44,6 +44,7 @@ Export-Package: org.eclipse.xtext.xtext.generator, org.eclipse.xtext.xtext.generator.scoping, org.eclipse.xtext.xtext.generator.types, org.eclipse.xtext.xtext.generator.ui.outline, + org.eclipse.xtext.xtext.generator.ui.quickfix, org.eclipse.xtext.xtext.generator.util, org.eclipse.xtext.xtext.generator.validation, org.eclipse.xtext.xtext.generator.web, diff --git a/plugins/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/ui/quickfix/QuickfixProviderFragment2.xtend b/plugins/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/ui/quickfix/QuickfixProviderFragment2.xtend new file mode 100644 index 000000000..ee189759b --- /dev/null +++ b/plugins/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/ui/quickfix/QuickfixProviderFragment2.xtend @@ -0,0 +1,177 @@ +/******************************************************************************* + * Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others. + * 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.xtext.xtext.generator.ui.quickfix + +import javax.inject.Inject +import org.eclipse.xtend.lib.annotations.Accessors +import org.eclipse.xtext.Grammar +import org.eclipse.xtext.xtext.generator.AbstractGeneratorFragment2 +import org.eclipse.xtext.xtext.generator.CodeConfig +import org.eclipse.xtext.xtext.generator.XtextGeneratorNaming +import org.eclipse.xtext.xtext.generator.model.FileAccessFactory +import org.eclipse.xtext.xtext.generator.model.GuiceModuleAccess +import org.eclipse.xtext.xtext.generator.model.TypeReference + +import static extension org.eclipse.xtext.xtext.generator.util.GrammarUtil2.* + +/** + * Contributes the Quickfix provider stub, either in Xtend or Java language. + * + * @author Christian Schneider - Initial contribution and API + */ +class QuickfixProviderFragment2 extends AbstractGeneratorFragment2 { + + @Inject + extension XtextGeneratorNaming + + @Inject + extension CodeConfig + + @Inject + FileAccessFactory fileAccessFactory + + @Accessors + private boolean generateStub = true; + + @Accessors + private boolean inheritImplementation; + + def protected TypeReference getQuickfixProviderClass(Grammar g) { + return new TypeReference( + grammar.eclipsePluginBasePackage + ".quickfix." + grammar.simpleName + "QuickfixProvider" + ) + } + + def protected TypeReference getQuickfixProviderSuperClass(Grammar g) { + val superGrammar = g.getNonTerminalsSuperGrammar; + if (inheritImplementation && superGrammar != null) + superGrammar.quickfixProviderClass + else + defaultQuickfixProviderSuperClass + } + + /** + * Extra getter facilitates customization by overriding. + */ + protected def getDefaultQuickfixProviderSuperClass() { + return new TypeReference("org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider") + } + + override generate() { + val instanceClass = + if (generateStub) grammar.quickfixProviderClass else grammar.quickfixProviderSuperClass; + + new GuiceModuleAccess.BindingFactory() + .addTypeToType( + new TypeReference("org.eclipse.xtext.ui.editor.quickfix.IssueResolutionProvider"), + instanceClass + ).contributeTo(language.eclipsePluginGenModule); + + if (!generateStub) { + return; + } + + if (projectConfig.eclipsePluginSrc !== null) { + if (preferXtendStubs) { + generateXtendQuickfixProvider + } else { + generateJavaQuickfixProvider + } + + projectConfig.eclipsePluginManifest.exportedPackages += grammar.quickfixProviderClass.packageName + + addRegistrationToPluginXml + } + } + + protected def generateXtendQuickfixProvider() { + fileAccessFactory.createXtendFile(grammar.quickfixProviderClass, ''' + //import org.eclipse.xtext.ui.editor.quickfix.Fix + //import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor + //import org.eclipse.xtext.validation.Issue + + /** + * Custom quickfixes. + * + * See https://www.eclipse.org/Xtext/documentation/304_ide_concepts.html#quick-fixes + */ + class «grammar.quickfixProviderClass.simpleName» extends «grammar.quickfixProviderSuperClass» { + + // @Fix(MyDslValidator::INVALID_NAME) + // def capitalizeName(Issue issue, IssueResolutionAcceptor acceptor) { + // acceptor.accept(issue, 'Capitalize name', 'Capitalize the name.', 'upcase.png') [ + // context | + // val xtextDocument = context.xtextDocument + // val firstLetter = xtextDocument.get(issue.offset, 1) + // xtextDocument.replace(issue.offset, 1, firstLetter.toUpperCase) + // ] + // } + } + ''').writeTo(projectConfig.eclipsePluginSrc) + } + + protected def generateJavaQuickfixProvider() { + fileAccessFactory.createJavaFile(grammar.quickfixProviderClass, ''' + /** + * Custom quickfixes. + * + * See https://www.eclipse.org/Xtext/documentation/304_ide_concepts.html#quick-fixes + */ + public class «grammar.quickfixProviderClass.simpleName» extends «grammar.quickfixProviderSuperClass» { + + // @Fix(MyJavaValidator.INVALID_NAME) + // public void capitalizeName(final Issue issue, IssueResolutionAcceptor acceptor) { + // acceptor.accept(issue, "Capitalize name", "Capitalize the name.", "upcase.png", new IModification() { + // public void apply(IModificationContext context) throws BadLocationException { + // IXtextDocument xtextDocument = context.getXtextDocument(); + // String firstLetter = xtextDocument.get(issue.getOffset(), 1); + // xtextDocument.replace(issue.getOffset(), 1, firstLetter.toUpperCase()); + // } + // }); + // } + + } + ''').writeTo(projectConfig.eclipsePluginSrc) + } + + protected def addRegistrationToPluginXml() { + val markerTypePrefix = grammar.eclipsePluginBasePackage + "." + grammar.simpleName.toLowerCase + val executableExtensionFactory = grammar.eclipsePluginExecutableExtensionFactory + + projectConfig.eclipsePluginPluginXml.entries += ''' + + + + + + + + + + + + + + + + ''' + } +} \ No newline at end of file diff --git a/plugins/org.eclipse.xtext.xtext.wizard/src/org/eclipse/xtext/xtext/wizard/RuntimeProjectDescriptor.xtend b/plugins/org.eclipse.xtext.xtext.wizard/src/org/eclipse/xtext/xtext/wizard/RuntimeProjectDescriptor.xtend index 2a3837e51..53daeb6ee 100644 --- a/plugins/org.eclipse.xtext.xtext.wizard/src/org/eclipse/xtext/xtext/wizard/RuntimeProjectDescriptor.xtend +++ b/plugins/org.eclipse.xtext.xtext.wizard/src/org/eclipse/xtext/xtext/wizard/RuntimeProjectDescriptor.xtend @@ -299,9 +299,7 @@ class RuntimeProjectDescriptor extends TestedProjectDescriptor { } // quickfix API - fragment = adapter.FragmentAdapter { - fragment = quickfix.QuickfixProviderFragment auto-inject {} - } + fragment = ui.quickfix.QuickfixProviderFragment2 auto-inject {} // content assist API fragment = adapter.FragmentAdapter { diff --git a/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.eclipsePlugin/org.xtext.example.eclipsePlugin/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 b/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.eclipsePlugin/org.xtext.example.eclipsePlugin/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 index 17c38695d..eb628080c 100644 --- a/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.eclipsePlugin/org.xtext.example.eclipsePlugin/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 +++ b/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.eclipsePlugin/org.xtext.example.eclipsePlugin/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 @@ -116,9 +116,7 @@ Workflow { } // quickfix API - fragment = adapter.FragmentAdapter { - fragment = quickfix.QuickfixProviderFragment auto-inject {} - } + fragment = ui.quickfix.QuickfixProviderFragment2 auto-inject {} // content assist API fragment = adapter.FragmentAdapter { diff --git a/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.full/org.xtext.example.full.parent/org.xtext.example.full/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 b/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.full/org.xtext.example.full.parent/org.xtext.example.full/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 index cb7dec168..d0e1c7532 100644 --- a/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.full/org.xtext.example.full.parent/org.xtext.example.full/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 +++ b/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.full/org.xtext.example.full.parent/org.xtext.example.full/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 @@ -124,9 +124,7 @@ Workflow { } // quickfix API - fragment = adapter.FragmentAdapter { - fragment = quickfix.QuickfixProviderFragment auto-inject {} - } + fragment = ui.quickfix.QuickfixProviderFragment2 auto-inject {} // content assist API fragment = adapter.FragmentAdapter { diff --git a/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.mavenTycho/org.xtext.example.mavenTycho.parent/org.xtext.example.mavenTycho/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 b/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.mavenTycho/org.xtext.example.mavenTycho.parent/org.xtext.example.mavenTycho/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 index 305893cde..27c83c38e 100644 --- a/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.mavenTycho/org.xtext.example.mavenTycho.parent/org.xtext.example.mavenTycho/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 +++ b/tests/org.eclipse.xtext.tests/testdata/wizard-expectations/org.xtext.example.mavenTycho/org.xtext.example.mavenTycho.parent/org.xtext.example.mavenTycho/src/org/xtext/example/mydsl/GenerateMyDsl.mwe2 @@ -120,9 +120,7 @@ Workflow { } // quickfix API - fragment = adapter.FragmentAdapter { - fragment = quickfix.QuickfixProviderFragment auto-inject {} - } + fragment = ui.quickfix.QuickfixProviderFragment2 auto-inject {} // content assist API fragment = adapter.FragmentAdapter {