org.eclipse.xtext.ui.editor.model.edit.IModification Java Examples

The following examples show how to use org.eclipse.xtext.ui.editor.model.edit.IModification. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FormatQuickfixProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Semantic quickfix setting the override flag for a rule.
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(FormatJavaValidator.OVERRIDE_MISSING_CODE)
public void setOverride(final Issue issue, final IssueResolutionAcceptor acceptor) {
  acceptor.accept(issue, "Set override", "Set override flag.", null, new IModification() {
    @Override
    public void apply(final IModificationContext context) throws BadLocationException {
      context.getXtextDocument().modify(new IUnitOfWork<Void, XtextResource>() {
        @Override
        public java.lang.Void exec(final XtextResource state) {
          Rule rule = (Rule) state.getEObject(issue.getUriToProblem().fragment());
          rule.setOverride(true);
          return null;
        }
      });
    }
  });
}
 
Example #2
Source File: CreateJavaTypeQuickfixes.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void newJavaInterfaceQuickfix(final String typeName, final String explicitPackage, final XtextResource resource, Issue issue,
		IssueResolutionAcceptor issueResolutionAcceptor) {
	String packageDescription = getPackageDescription(explicitPackage);
	issueResolutionAcceptor.accept(issue, "Create Java interface '" + typeName + "'" + packageDescription,
			"Opens the new Java interface wizard to create the type '" + typeName + "'" + packageDescription,
			"java_interface.gif", new IModification() {
				@Override
				public void apply(/* @Nullable */ IModificationContext context) throws Exception {
					runAsyncInDisplayThread(new Runnable() {
						@Override
						public void run() {
							NewInterfaceWizardPage classWizardPage = new NewInterfaceWizardPage();
							NewInterfaceCreationWizard wizard = new NewInterfaceCreationWizard(classWizardPage, true);
							WizardDialog dialog = createWizardDialog(wizard);
							configureWizardPage(classWizardPage, resource.getURI(), typeName, explicitPackage);
							dialog.open();
						}
					});
				}
			});
}
 
Example #3
Source File: XbaseQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Fix(IssueCodes.OBSOLETE_CAST)
public void fixObsoletCast(final Issue issue, IssueResolutionAcceptor acceptor) {
	String fixup = "Remove unnecessary cast";
	acceptor.accept(issue, fixup, fixup, null, new IModification() {
		@Override
		public void apply(IModificationContext context) throws Exception {
			final IXtextDocument document = context.getXtextDocument();
			ReplaceRegion replacement = document.tryReadOnly(new IUnitOfWork<ReplaceRegion, XtextResource>() {

				@Override
				public ReplaceRegion exec(XtextResource state) throws Exception {
					EObject type = state.getEObject(issue.getUriToProblem().fragment());
					XCastedExpression cast = EcoreUtil2.getContainerOfType(type, XCastedExpression.class);
					INode castNode = NodeModelUtils.findActualNodeFor(cast);
					INode targetNode = NodeModelUtils.findActualNodeFor(cast.getTarget());
					return new ReplaceRegion(castNode.getTotalTextRegion(), targetNode.getText());
				}
			});
			if (replacement != null) {
				document.replace(replacement.getOffset(), replacement.getLength(), replacement.getText());
			}
		}
	});
}
 
Example #4
Source File: CreateJavaTypeQuickfixes.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void newJavaAnnotationQuickfix(final String typeName, final String explicitPackage, final XtextResource resource, Issue issue,
		IssueResolutionAcceptor issueResolutionAcceptor) {
	String packageDescription = getPackageDescription(explicitPackage);
	issueResolutionAcceptor.accept(issue, "Create Java annotation '@" + typeName + "'" + packageDescription,
			"Opens the new Java annotation wizard to create the type '@" + typeName + "'" + packageDescription, "java_file.gif",
			new IModification() {
				@Override
				public void apply(/* @Nullable */ IModificationContext context) throws Exception {
					runAsyncInDisplayThread(new Runnable() {
						@Override
						public void run() {
							NewAnnotationWizardPage annotationWizardPage = new NewAnnotationWizardPage();
							NewAnnotationCreationWizard wizard = new NewAnnotationCreationWizard(annotationWizardPage, true);
							WizardDialog dialog = createWizardDialog(wizard);
							configureWizardPage(annotationWizardPage, resource.getURI(), typeName, explicitPackage);
							dialog.open();
						}
					});
				}
			});
}
 
Example #5
Source File: CreateXtendTypeQuickfixes.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void newXtendClassQuickfix(final String typeName, final String explicitPackage, final XtextResource resource, Issue issue,
		IssueResolutionAcceptor issueResolutionAcceptor) {
	String packageDescription = getPackageDescription(explicitPackage);
	issueResolutionAcceptor.accept(issue, "Create Xtend class '" + typeName + "'" + packageDescription,
			"Opens the new Xtend class wizard to create the type '" + typeName + "'" + packageDescription, "xtend_file.png",
			new IModification() {
				@Override
				public void apply(/* @Nullable */ IModificationContext context) throws Exception {
					runAsyncInDisplayThread(new Runnable() {
						@Override
						public void run() {
							NewElementWizard newXtendClassWizard = newXtendClassWizardProvider.get();
							WizardDialog dialog = createWizardDialog(newXtendClassWizard);
							NewXtendClassWizardPage page = (NewXtendClassWizardPage) newXtendClassWizard.getStartingPage();
							configureWizardPage(page, resource.getURI(), typeName, explicitPackage);
							dialog.open();
						}
					});
				}
			});
}
 
Example #6
Source File: CreateXtendTypeQuickfixes.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void newXtendInterfaceQuickfix(final String typeName, final String explicitPackage, final XtextResource resource, Issue issue,
		IssueResolutionAcceptor issueResolutionAcceptor) {
	String packageDescription = getPackageDescription(explicitPackage);
	issueResolutionAcceptor.accept(issue, "Create Xtend interface '" + typeName + "'" + packageDescription,
			"Opens the new Xtend interface wizard to create the type '" + typeName + "'" + packageDescription, "xtend_file.png",
			new IModification() {
				@Override
				public void apply(/* @Nullable */ IModificationContext context) throws Exception {
					runAsyncInDisplayThread(new Runnable() {
						@Override
						public void run() {
							NewElementWizard newXtendInterfaceWizard = newXtendInterfaceWizardProvider.get();
							WizardDialog dialog = createWizardDialog(newXtendInterfaceWizard);
							NewXtendInterfaceWizardPage page = (NewXtendInterfaceWizardPage) newXtendInterfaceWizard.getStartingPage();
							configureWizardPage(page, resource.getURI(), typeName, explicitPackage);
							dialog.open();
						}
					});
				}
			});
}
 
Example #7
Source File: CreateXtendTypeQuickfixes.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void newXtendAnnotationQuickfix(final String typeName, final String explicitPackage, final XtextResource resource, Issue issue,
		IssueResolutionAcceptor issueResolutionAcceptor) {
	String packageDescription = getPackageDescription(explicitPackage);
	issueResolutionAcceptor.accept(issue, "Create Xtend annotation '" + typeName + "'" + packageDescription,
			"Opens the new Xtend annotation wizard to create the type '" + typeName + "'" + packageDescription, "xtend_file.png",
			new IModification() {
				@Override
				public void apply(/* @Nullable */ IModificationContext context) throws Exception {
					runAsyncInDisplayThread(new Runnable() {
						@Override
						public void run() {
							NewElementWizard newXtendAnnotationWizard = newXtendAnnotationWizardProvider.get();
							WizardDialog dialog = createWizardDialog(newXtendAnnotationWizard);
							NewXtendAnnotationWizardPage page = (NewXtendAnnotationWizardPage) newXtendAnnotationWizard.getStartingPage();
							configureWizardPage(page, resource.getURI(), typeName, explicitPackage);
							dialog.open();
						}
					});
				}
			});
}
 
Example #8
Source File: CodeBuilderQuickfix.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public void addQuickfix(final ICodeBuilder builder, final String label, final Issue issue, final IssueResolutionAcceptor acceptor) {
  boolean _isValid = builder.isValid();
  if (_isValid) {
    IModification _switchResult = null;
    boolean _matched = false;
    if (builder instanceof ICodeBuilder.Xtend) {
      _matched=true;
      _switchResult = this.getXtendModification(((ICodeBuilder.Xtend)builder));
    }
    if (!_matched) {
      if (builder instanceof ICodeBuilder.Java) {
        _matched=true;
        _switchResult = this.getJavaModification(((ICodeBuilder.Java)builder));
      }
    }
    final IModification modification = _switchResult;
    acceptor.accept(issue, label, builder.getPreview(), builder.getImage(), modification);
  }
}
 
Example #9
Source File: FormatQuickfixProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Semantic quickfix removing the override flag for a rule.
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(FormatJavaValidator.OVERRIDE_ILLEGAL_CODE)
public void removeOverride(final Issue issue, final IssueResolutionAcceptor acceptor) {
  acceptor.accept(issue, "Remove override", "Remove override.", null, new IModification() {
    @Override
    public void apply(final IModificationContext context) throws BadLocationException {
      context.getXtextDocument().modify(new IUnitOfWork<Void, XtextResource>() {
        @Override
        public java.lang.Void exec(final XtextResource state) {
          Rule rule = (Rule) state.getEObject(issue.getUriToProblem().fragment());
          rule.setOverride(false);
          return null;
        }
      });
    }
  });
}
 
Example #10
Source File: CheckQuickfixProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Fix catalog name.
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(IssueCodes.WRONG_FILE)
public void fixCatalogName(final Issue issue, final IssueResolutionAcceptor acceptor) {
  acceptor.accept(issue, Messages.CheckQuickfixProvider_CORRECT_CATALOG_NAME_LABEL, Messages.CheckQuickfixProvider_CORRECT_CATALOG_NAME_DESCN, NO_IMAGE, new IModification() {
    @Override
    public void apply(final IModificationContext context) throws BadLocationException {
      IXtextDocument xtextDocument = context.getXtextDocument();
      IFile file = xtextDocument.getAdapter(IFile.class);
      if (file != null) {
        final String fileName = file.getName();
        final String name = fileName.indexOf('.') > 0 ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName;
        xtextDocument.replace(issue.getOffset(), issue.getLength(), name);
      }
    }
  });
}
 
Example #11
Source File: CheckQuickfixProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Fixes an illegally set default severity. The default severity must be within given severity range.
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(IssueCodes.DEFAULT_SEVERITY_NOT_IN_RANGE)
public void fixIllegalDefaultSeverity(final Issue issue, final IssueResolutionAcceptor acceptor) {
  if (issue.getData() != null) {
    for (final String severityProposal : issue.getData()) {
      final String label = NLS.bind(Messages.CheckQuickfixProvider_DEFAULT_SEVERITY_FIX_LABEL, severityProposal);
      final String descn = NLS.bind(Messages.CheckQuickfixProvider_DEFAULT_SEVERITY_FIX_DESCN, severityProposal);

      acceptor.accept(issue, label, descn, NO_IMAGE, new IModification() {
        @Override
        public void apply(final IModificationContext context) throws BadLocationException {
          IXtextDocument xtextDocument = context.getXtextDocument();
          xtextDocument.replace(issue.getOffset(), issue.getLength(), severityProposal);
        }
      });
    }
  }
}
 
Example #12
Source File: XtextGrammarQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Fix(INVALID_PACKAGE_REFERENCE_INHERITED)
public void fixImportedPackageFromSuperGrammar(final Issue issue, IssueResolutionAcceptor acceptor) {
	if (issue.getData().length == 1)
		acceptor.accept(issue, 
				"Change to '" + issue.getData()[0] + "'", 
				"Fix the bogus package import\n" +
				"import '" + issue.getData()[0] + "'", NULL_QUICKFIX_IMAGE,
				new IModification() {
					@Override
					public void apply(IModificationContext context) throws BadLocationException {
						String replaceString = valueConverterService.toString(issue.getData()[0], "STRING");
						IXtextDocument document = context.getXtextDocument();
						String delimiter = document.get(issue.getOffset(), 1);
						if (!replaceString.startsWith(delimiter)) {
							replaceString = delimiter + replaceString.substring(1, replaceString.length() - 1) + delimiter; 
						}
						document.replace(issue.getOffset(), issue.getLength(), replaceString);
					}
				});
}
 
Example #13
Source File: CheckCfgQuickfixProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Fix severity by setting it to a legal value as is defined by severity range of referenced check. Legal
 * severities are passed as issue data (org.eclipse.xtext.validation.Issue#getData()).
 * 
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(IssueCodes.SEVERITY_NOT_ALLOWED)
public void fixSeverityToMaxSeverity(final Issue issue, final IssueResolutionAcceptor acceptor) {
  if (issue.getData() != null) {
    for (final String severityProposal : issue.getData()) {
      final String label = NLS.bind(Messages.CheckCfgQuickfixProvider_CORRECT_SEVERITY_LABEL, severityProposal);
      final String descn = NLS.bind(Messages.CheckCfgQuickfixProvider_CORRECT_SEVERITY_DESCN, severityProposal);

      acceptor.accept(issue, label, descn, NO_IMAGE, new IModification() {
        public void apply(final IModificationContext context) throws BadLocationException {
          IXtextDocument xtextDocument = context.getXtextDocument();
          xtextDocument.replace(issue.getOffset(), issue.getLength(), severityProposal);
        }
      });
    }
  }
}
 
Example #14
Source File: XtextGrammarQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Fix(EMPTY_KEYWORD)
public void replaceEmptyKeywordWithRuleName(final Issue issue, IssueResolutionAcceptor acceptor) {
	acceptor.accept(issue, "Replace empty keyword with rule name", "Replace empty keyword with rule name", NULL_QUICKFIX_IMAGE,
			new IModification() {
				@Override
				public void apply(IModificationContext context) throws Exception {
					final IXtextDocument document = context.getXtextDocument();
					final String containingRuleName = document.tryPriorityReadOnly(new IUnitOfWork<String, XtextResource>() {
						@Override
						public String exec(XtextResource state) throws Exception {
							return Optional.ofNullable(issue.getUriToProblem().fragment()).map(state::getEObject)
									.map(GrammarUtil::containingRule).map(AbstractRule::getName).map(Strings::toFirstLower)
									.orElse(null);
						}
					});
					if (containingRuleName != null) {
						final String quote = String.valueOf(document.getChar(issue.getOffset()));
						document.replace(issue.getOffset(), issue.getLength(), quote + containingRuleName + quote);
					}
				}
			});
}
 
Example #15
Source File: CheckQuickfixProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Replaces a guard by an if-expression.
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(IssueCodes.GUARDS_DEPRECATED)
public void fixGuardStatement(final Issue issue, final IssueResolutionAcceptor acceptor) {
  acceptor.accept(issue, Messages.CheckQuickfixProvider_GUARD_DEPRECATED_LABEL, Messages.CheckQuickfixProvider_GUARD_DEPRECATED_DESCN, NO_IMAGE, new IModification() {
    @Override
    public void apply(final IModificationContext context) throws BadLocationException {
      IXtextDocument xtextDocument = context.getXtextDocument();
      String expr = xtextDocument.get(issue.getOffset(), issue.getLength());
      expr = expr.replaceFirst("^guard(\\s*)", "if (!(") + ")) {return}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      xtextDocument.replace(issue.getOffset(), issue.getLength(), expr);
    }
  });
}
 
Example #16
Source File: FormatQuickfixProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Syntactic quickfix creating a rule.
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(FormatJavaValidator.GRAMMAR_RULE_MISSING_CODE)
public void addGrammarRule(final Issue issue, final IssueResolutionAcceptor acceptor) {
  acceptor.accept(issue, "Create rule", "Create rule " + issue.getData()[0], null, new IModification() {
    @Override
    public void apply(final IModificationContext context) throws BadLocationException {
      final IXtextDocument xtextDocument = context.getXtextDocument();
      xtextDocument.replace(xtextDocument.getLength(), 0, "\n" + issue.getData()[0] + " {\n\t\n}");
    }
  });
}
 
Example #17
Source File: XbaseQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void organizeImports(final Issue issue, IssueResolutionAcceptor acceptor) {
	acceptor.accept(issue, "Organize imports",
			"Organizes the whole import section. Removes wildcard imports as well as duplicates and unused ones.",
			getOrganizeImportsImage(), new IModification() {
				@Override
				public void apply(IModificationContext context) throws Exception {
					organizeImportsHandler.doOrganizeImports(context.getXtextDocument());
				}
			});
}
 
Example #18
Source File: CheckQuickfixProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Fixes the wrong package name.
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(IssueCodes.WRONG_PACKAGE)
public void fixPackageName(final Issue issue, final IssueResolutionAcceptor acceptor) {
  acceptor.accept(issue, Messages.CheckQuickfixProvider_CORRECT_PKG_NAME_LABEL, Messages.CheckQuickfixProvider_CORRECT_PKG_NAME_DESCN, NO_IMAGE, new IModification() {
    @Override
    public void apply(final IModificationContext context) throws BadLocationException {
      IXtextDocument xtextDocument = context.getXtextDocument();
      final String packageName = resourceUtil.getNameOfContainingPackage(xtextDocument);
      if (packageName != null) {
        xtextDocument.replace(issue.getOffset(), issue.getLength(), packageName);
      }
    }
  });
}
 
Example #19
Source File: CheckQuickfixProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Adds an {@code issue} statement to a constraint. If the constraint already contains other expressions, {@code issue} is appended to the end.
 *
 * @param issue
 *          the issue
 * @param acceptor
 *          the acceptor
 */
@Fix(IssueCodes.MISSING_ISSUE_EXPRESSION)
public void addIssueExpression(final Issue issue, final IssueResolutionAcceptor acceptor) {
  acceptor.accept(issue, Messages.CheckQuickfixProvider_ADD_ISSUE_EXPR_LABEL, Messages.CheckQuickfixProvider_ADD_ISSUE_EXPR_DESCN, NO_IMAGE, new IModification() {
    @Override
    public void apply(final IModificationContext context) throws BadLocationException {
      IXtextDocument xtextDocument = context.getXtextDocument();
      xtextDocument.replace(issue.getOffset() + issue.getLength() - 1, 0, ' ' + "issue" + ' '); //$NON-NLS-1$
    }
  });
}
 
Example #20
Source File: XtendQuickfixProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Fix(IssueCodes.WRONG_FILE)
public void fixFileName(final Issue issue, IssueResolutionAcceptor acceptor) {
	if (issue.getData() != null && issue.getData().length == 1) {
		final String expectedFileName = issue.getData()[0];
		final IFile iFile = projectUtil.findFileStorage(issue.getUriToProblem(), true);
		final IPath pathToMoveTo = iFile.getParent().getFullPath().append(expectedFileName)
				.addFileExtension(iFile.getFileExtension());
		if (!iFile.getWorkspace().getRoot().exists(pathToMoveTo)) {
			final String label = "Rename file to '" + expectedFileName + ".xtend'";
			acceptor.accept(issue, label, label, "xtend_file.png", new IModification() {
				@Override
				public void apply(IModificationContext context) throws Exception {
					runAsyncInDisplayThread(new Runnable() {
						@Override
						public void run() {
							try {
								iFile.move(pathToMoveTo, IResource.KEEP_HISTORY, null);
							} catch (CoreException e) {
								logger.error(e);
							}
						}
					});
				}
			});
		}
	}
}
 
Example #21
Source File: XtendQuickfixProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Fix(IssueCodes.OBSOLETE_ANNOTATION_OVERRIDE)
public void fixObsoleteOverrideAnnotation(final Issue issue, IssueResolutionAcceptor acceptor) {
	acceptor.accept(issue, "Remove superfluous @Override", "Removes superfluous @Override annotation from this function", "fix_indent.gif",
		new IModification() {
			@Override
			public void apply(IModificationContext context) throws Exception {
				context.getXtextDocument().replace(issue.getOffset(), issue.getLength(), "");
			}
	});
}
 
Example #22
Source File: XtendQuickfixProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Fix(IssueCodes.INCONSISTENT_INDENTATION)
public void fixIndentation(final Issue issue, IssueResolutionAcceptor acceptor) {
	acceptor.accept(issue, "Correct indentation", "Correctly indents this line in this rich string",
			"fix_indent.gif", new IModification() {
				@Override
				public void apply(IModificationContext context) throws Exception {
					context.getXtextDocument().replace(issue.getOffset(), issue.getLength(), issue.getData()[0]);
				}
			});
}
 
Example #23
Source File: GamlQuickfixProvider.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Fix (IGamlIssue.NO_INIT)
public void addInit(final Issue issue, final IssueResolutionAcceptor acceptor) {
	acceptor.accept(issue, "Add an init facet...", "", "", (IModification) context -> {
		final IXtextDocument doc = context.getXtextDocument();
		doc.replace(issue.getOffset() + issue.getLength() + 1, 0, " <- " + issue.getData()[0] + " ");

	});
}
 
Example #24
Source File: GamlQuickfixProvider.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Fix (IGamlIssue.IMPORT_ERROR)
public void gotoImport(final Issue issue, final IssueResolutionAcceptor acceptor) {
	final String[] data = issue.getData();
	if (data == null || data.length == 0) { return; }
	final String path = data[0];
	final URI uri = URI.createURI(path, false);
	acceptor.accept(issue, "Open " + uri.lastSegment() + "...", "Open file " + uri.lastSegment() + " to fix it", "",
			(IModification) context -> FileOpener.openFile(uri));

}
 
Example #25
Source File: XbaseQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Fix(IssueCodes.INVALID_TYPE_ARGUMENTS_ON_TYPE_LITERAL)
public void fixTypeArguments(final Issue issue, IssueResolutionAcceptor acceptor) {
	String message = issue.getMessage();
	String fixup = "Remove invalid type arguments";
	if (message.contains("argument.")) {
		fixup = "Remove invalid type argument";
	}
	acceptor.accept(issue, fixup, fixup, null, new IModification() {
		@Override
		public void apply(IModificationContext context) throws Exception {
			IXtextDocument document = context.getXtextDocument();
			document.replace(issue.getOffset(), issue.getLength(), "");
		}
	});
}
 
Example #26
Source File: XtextGrammarQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Fix(EMPTY_KEYWORD)
public void removeEmptyKeyword(final Issue issue, IssueResolutionAcceptor acceptor) {
	acceptor.accept(issue, "Remove empty keyword", "Remove empty keyword", NULL_QUICKFIX_IMAGE, new IModification() {
		@Override
		public void apply(IModificationContext context) throws Exception {
			context.getXtextDocument().replace(issue.getOffset(), issue.getLength(), "");
		}
	});

}
 
Example #27
Source File: XtextGrammarQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Fix(SPACES_IN_KEYWORD)
public void fixKeywordNoSpaces(final Issue issue, IssueResolutionAcceptor acceptor) {
	acceptor.accept(issue, "Fix keyword with spaces", "Fix keyword with spaces", NULL_QUICKFIX_IMAGE, new IModification() {
		@Override
		public void apply(IModificationContext context) throws Exception {
			final int offset = issue.getOffset();
			final int length = issue.getLength();
			final IXtextDocument document = context.getXtextDocument();
			final String quote = String.valueOf(document.getChar(offset));
			final String identifiers = document.get(offset, length).replaceAll("'|\"", "").trim();
			if (!Strings.isEmpty(identifiers)) {
				document.replace(offset, length, quote + Joiner.on(quote + " " + quote).join(identifiers.split("\\s+")) + quote);
			} else {
				final String containingRuleName = document.tryPriorityReadOnly(new IUnitOfWork<String, XtextResource>() {
					@Override
					public String exec(XtextResource state) throws Exception {
						return Optional.ofNullable(issue.getUriToProblem().fragment()).map(state::getEObject)
								.map(GrammarUtil::containingRule).map(AbstractRule::getName).map(Strings::toFirstLower)
								.orElse(null);
					}
				});
				if (containingRuleName != null) {
					document.replace(offset, length, quote + containingRuleName + quote);
				}
			}
		}
	});
}
 
Example #28
Source File: XtextGrammarQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Fix(INVALID_ACTION_USAGE)
public void fixInvalidActionUsage(final Issue issue, IssueResolutionAcceptor acceptor) {
	acceptor.accept(issue, "Fix invalid action usage", "Fix invalid action usage", NULL_QUICKFIX_IMAGE,
			new IModification() {
				@Override
				public void apply(IModificationContext context) throws BadLocationException {
					context.getXtextDocument().replace(issue.getOffset(), issue.getLength(), "");
				}
			});
}
 
Example #29
Source File: XtextGrammarQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Fix(EXPLICIT_OVERRIDE_MISSING)
public void addOverrideTag(final Issue issue, IssueResolutionAcceptor acceptor) {
	acceptor.accept(issue, "Add missing @Override annotation", "Inserts the missing @Override annotation.", null, new IModification() {
		
		@Override
		public void apply(IModificationContext context) throws Exception {
			final URI uri= issue.getUriToProblem();
			final IXtextDocument document = context.getXtextDocument(uri);
			
			if (document == null) {
				return;
			}
			
			final Integer offset = document.tryReadOnly(new IUnitOfWork<Integer, XtextResource>() {
				
				@Override
				public Integer exec(XtextResource state) throws Exception {
					final EObject eObject = state.getEObject(uri.fragment());
					if (eObject == null) {
						return null;
					} else {
						return NodeModelUtils.findActualNodeFor(eObject).getOffset();
					}
				}
			});
			
			if (offset != null) {
				document.replace(offset.intValue(), 0, "@Override " + separatorInfo.getLineSeparator());
			}
		}
	});
}
 
Example #30
Source File: StatemachineQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Fix(StatemachineValidator.INVALID_NAME)
public void convertNameToFirstLowerCase(Issue issue, IssueResolutionAcceptor acceptor) {
	String[] data = issue.getData();
	String firstLower = StringExtensions.toFirstLower(data[0]);
	String label = "Change to \'" + firstLower + "\'.";
	acceptor.accept(issue, label, label, "upcase.png", (IModification) (IModificationContext ctx) -> {
		IXtextDocument xtextDocument = ctx.getXtextDocument();
		String firstLetter = xtextDocument.get(issue.getOffset(), 1);
		xtextDocument.replace(issue.getOffset(), 1, firstLetter.toLowerCase());
	});
}