org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext Java Examples

The following examples show how to use org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext. 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: XtextQuickAssistProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.3
 */
protected void selectAndRevealQuickfix(IQuickAssistInvocationContext invocationContext, Set<Annotation> applicableAnnotations, List<ICompletionProposal> completionProposals) {
       if (completionProposals.isEmpty()) {
       	return;
       }
	if (!(invocationContext instanceof QuickAssistInvocationContext && ((QuickAssistInvocationContext) invocationContext).isSuppressSelection())) {
		ISourceViewer sourceViewer = invocationContext.getSourceViewer();
		IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
		Iterator<Annotation> iterator = applicableAnnotations.iterator();
		while (iterator.hasNext()) {
			Position pos = annotationModel.getPosition(iterator.next());
			if (pos != null) {
				sourceViewer.setSelectedRange(pos.getOffset(), pos.getLength());
				sourceViewer.revealRange(pos.getOffset(), pos.getLength());
				break;
			}
		}
	}
}
 
Example #2
Source File: TexSpellingEngine.java    From texlipse with Eclipse Public License 1.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public ICompletionProposal[] getProposals(IQuickAssistInvocationContext context) {
    List<Word> sugg = fError.getSuggestions();
    int length = fError.getInvalidWord().length();
    ICompletionProposal[] props = new ICompletionProposal[sugg.size() + 2];
    for (int i=0; i < sugg.size(); i++) {
        String suggestion = sugg.get(i).toString();
        String s = MessageFormat.format(CHANGE_TO,
                new Object[] { suggestion });
        props[i] = new CompletionProposal(suggestion, 
                fOffset, length, suggestion.length(), fCorrectionImage, s, null, null);
    }
    props[props.length - 2] = new IgnoreProposal(ignore, fError.getInvalidWord(), context.getSourceViewer());
    props[props.length - 1] = new AddToDictProposal(fError, fLang, context.getSourceViewer());
    return props;
}
 
Example #3
Source File: WordCorrectionProposal.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a new word correction proposal.
 *
 * @param word the corrected word
 * @param arguments the problem arguments associated with the spelling problem
 * @param offset the offset in the document where to apply the proposal
 * @param length the lenght in the document to apply the proposal
 * @param context the invocation context for this proposal
 * @param relevance the relevance of this proposal
 */
public WordCorrectionProposal(final String word, final String[] arguments, final int offset, final int length, final IQuickAssistInvocationContext context, final int relevance) {

	fWord= Character.isUpperCase(arguments[0].charAt(0)) ? Character.toUpperCase(word.charAt(0)) + word.substring(1) : word;

	fOffset= offset;
	fLength= length;
	fContext= context;
	fRelevance= relevance;

	final StringBuffer buffer= new StringBuffer(80);

	buffer.append("...<br>"); //$NON-NLS-1$
	buffer.append(getHtmlRepresentation(arguments[1]));
	buffer.append("<b>"); //$NON-NLS-1$
	buffer.append(getHtmlRepresentation(fWord));
	buffer.append("</b>"); //$NON-NLS-1$
	buffer.append(getHtmlRepresentation(arguments[2]));
	buffer.append("<br>..."); //$NON-NLS-1$

	fLine= buffer.toString();
}
 
Example #4
Source File: WordCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a new word correction proposal.
 *
 * @param word the corrected word
 * @param arguments the problem arguments associated with the spelling problem
 * @param offset the offset in the document where to apply the proposal
 * @param length the lenght in the document to apply the proposal
 * @param context the invocation context for this proposal
 * @param relevance the relevance of this proposal
 */
public WordCorrectionProposal(final String word, final String[] arguments, final int offset, final int length, final IQuickAssistInvocationContext context, final int relevance) {

	fWord= Character.isUpperCase(arguments[0].charAt(0)) ? Character.toUpperCase(word.charAt(0)) + word.substring(1) : word;

	fOffset= offset;
	fLength= length;
	fContext= context;
	fRelevance= relevance;

	final StringBuffer buffer= new StringBuffer(80);

	buffer.append("...<br>"); //$NON-NLS-1$
	buffer.append(getHtmlRepresentation(arguments[1]));
	buffer.append("<b>"); //$NON-NLS-1$
	buffer.append(getHtmlRepresentation(fWord));
	buffer.append("</b>"); //$NON-NLS-1$
	buffer.append(getHtmlRepresentation(arguments[2]));
	buffer.append("<br>..."); //$NON-NLS-1$

	fLine= buffer.toString();
}
 
Example #5
Source File: JsonQuickAssistProcessor.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
    List<IMarker> markers;
    try {
        markers = getMarkersFor(invocationContext.getSourceViewer(), invocationContext.getOffset());
    } catch (BadLocationException e) {
        errorMessage = e.getMessage();
        return new ICompletionProposal[0];
    }

    List<MarkerResolutionProposal> result = markers.stream() //
            .flatMap(e -> generators.stream()
                    .flatMap(generator -> Stream.of(generator.getResolutions(e))
                            .map(m -> new MarkerResolutionProposal(e, m, invocationContext.getSourceViewer()))))
            .collect(Collectors.toList());

    return result.toArray(new ICompletionProposal[result.size()]);
}
 
Example #6
Source File: XtextQuickAssistProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.3
 */
protected List<ICompletionProposal> createQuickfixes(IQuickAssistInvocationContext invocationContext, Set<Annotation> applicableAnnotations) {
   	List<ICompletionProposal> result = Lists.newArrayList();
   	ISourceViewer sourceViewer = invocationContext.getSourceViewer();
	IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
	IXtextDocument xtextDocument = xtextDocumentUtil.getXtextDocument(sourceViewer);
   	for(Annotation annotation : applicableAnnotations) {
		if (annotation instanceof SpellingAnnotation) {
			SpellingProblem spellingProblem = ((SpellingAnnotation) annotation).getSpellingProblem();
			ICompletionProposal[] proposals = spellingProblem.getProposals();
			if (proposals != null) {
				result.addAll(asList(proposals));
			}
		} else {
			final Issue issue = issueUtil.getIssueFromAnnotation(annotation);
			Position pos = annotationModel.getPosition(annotation);
			if (issue != null && pos != null) {
				@SuppressWarnings("deprecation")
				Iterable<IssueResolution> resolutions = getResolutions(issue, xtextDocument);
				if (resolutions.iterator().hasNext()) {
					for (IssueResolution resolution : resolutions) {
						result.add(create(pos, resolution));
					}
				}
			}
		}
	}
   	return result;
   }
 
Example #7
Source File: JavaCorrectionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext quickAssistContext) {
	ISourceViewer viewer= quickAssistContext.getSourceViewer();
	int documentOffset= quickAssistContext.getOffset();

	IEditorPart part= fAssistant.getEditor();

	ICompilationUnit cu= JavaUI.getWorkingCopyManager().getWorkingCopy(part.getEditorInput());
	IAnnotationModel model= JavaUI.getDocumentProvider().getAnnotationModel(part.getEditorInput());

	AssistContext context= null;
	if (cu != null) {
		int length= viewer != null ? viewer.getSelectedRange().y : 0;
		context= new AssistContext(cu, viewer, part, documentOffset, length);
	}
	
	Annotation[] annotations= fAssistant.getAnnotationsAtOffset();

	fErrorMessage= null;

	ICompletionProposal[] res= null;
	if (model != null && context != null && annotations != null) {
		ArrayList<IJavaCompletionProposal> proposals= new ArrayList<IJavaCompletionProposal>(10);
		IStatus status= collectProposals(context, model, annotations, true, !fAssistant.isUpdatedOffset(), proposals);
		res= proposals.toArray(new ICompletionProposal[proposals.size()]);
		if (!status.isOK()) {
			fErrorMessage= status.getMessage();
			JavaPlugin.log(status);
		}
	}

	if (res == null || res.length == 0) {
		return new ICompletionProposal[] { new ChangeCorrectionProposal(CorrectionMessages.NoCorrectionProposal_description, new NullChange(""), IProposalRelevance.NO_SUGGESSTIONS_AVAILABLE, null) }; //$NON-NLS-1$
	}
	if (res.length > 1) {
		Arrays.sort(res, new CompletionProposalComparator());
	}
	return res;
}
 
Example #8
Source File: PropertiesCorrectionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private PropertiesAssistContext createAssistContext(IQuickAssistInvocationContext invocationContext) {
	IEditorPart editorPart= fAssistant.getEditor();
	IFile file= (IFile) editorPart.getEditorInput().getAdapter(IFile.class);
	ISourceViewer sourceViewer= invocationContext.getSourceViewer();
	IType accessorType= ((PropertiesFileEditor) editorPart).getAccessorType();
	return new PropertiesAssistContext(sourceViewer, invocationContext.getOffset(), invocationContext.getLength(), file, sourceViewer.getDocument(), accessorType);
}
 
Example #9
Source File: PropertiesQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean getEscapeUnescapeBackslashProposals(IQuickAssistInvocationContext invocationContext, ArrayList<ICompletionProposal> resultingCollections) throws BadLocationException,
		BadPartitioningException {
	ISourceViewer sourceViewer= invocationContext.getSourceViewer();
	IDocument document= sourceViewer.getDocument();
	Point selectedRange= sourceViewer.getSelectedRange();
	int selectionOffset= selectedRange.x;
	int selectionLength= selectedRange.y;
	int proposalOffset;
	int proposalLength;
	String text;
	if (selectionLength == 0) {
		if (selectionOffset != document.getLength()) {
			char ch= document.getChar(selectionOffset);
			if (ch == '=' || ch == ':') { //see PropertiesFilePartitionScanner()
				return false;
			}
		}

		ITypedRegion partition= null;
		if (document instanceof IDocumentExtension3)
			partition= ((IDocumentExtension3)document).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, invocationContext.getOffset(), false);
		if (partition == null)
			return false;

		String type= partition.getType();
		if (!(type.equals(IPropertiesFilePartitions.PROPERTY_VALUE) || type.equals(IDocument.DEFAULT_CONTENT_TYPE))) {
			return false;
		}
		proposalOffset= partition.getOffset();
		proposalLength= partition.getLength();
		text= document.get(proposalOffset, proposalLength);

		if (type.equals(IPropertiesFilePartitions.PROPERTY_VALUE)) {
			text= text.substring(1); //see PropertiesFilePartitionScanner()
			proposalOffset++;
			proposalLength--;
		}
	} else {
		proposalOffset= selectionOffset;
		proposalLength= selectionLength;
		text= document.get(proposalOffset, proposalLength);
	}

	if (PropertiesFileEscapes.containsUnescapedBackslash(text)) {
		if (resultingCollections == null)
			return true;
		resultingCollections.add(new EscapeBackslashCompletionProposal(PropertiesFileEscapes.escape(text, false, true, false), proposalOffset, proposalLength,
				PropertiesFileEditorMessages.EscapeBackslashCompletionProposal_escapeBackslashes));
		return true;
	}
	if (PropertiesFileEscapes.containsEscapedBackslashes(text)) {
		if (resultingCollections == null)
			return true;
		resultingCollections.add(new EscapeBackslashCompletionProposal(PropertiesFileEscapes.unescapeBackslashes(text), proposalOffset, proposalLength,
				PropertiesFileEditorMessages.EscapeBackslashCompletionProposal_unescapeBackslashes));
		return true;
	}
	return false;
}
 
Example #10
Source File: ScriptConsoleViewerWrapper.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public IQuickAssistInvocationContext getQuickAssistInvocationContext() {
    return viewer.getQuickAssistInvocationContext();
}
 
Example #11
Source File: PydevConsoleQuickAssistProcessor.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Computes quick assists for the console.
 */
@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
    ISourceViewer sourceViewer = invocationContext.getSourceViewer();
    List<ICompletionProposalHandle> props = new ArrayList<ICompletionProposalHandle>();
    if (sourceViewer instanceof ScriptConsoleViewer) {
        ScriptConsoleViewer viewer = (ScriptConsoleViewer) sourceViewer;

        //currently, only the assign quick assist is used
        AssistAssign assistAssign = new AssistAssign();

        ISelection selection = sourceViewer.getSelectionProvider().getSelection();
        if (selection instanceof ITextSelection) {
            PySelection ps = PySelectionFromEditor.createPySelectionFromEditor(sourceViewer,
                    (ITextSelection) selection);
            int offset = viewer.getCaretOffset();
            String commandLine = viewer.getCommandLine();

            //let's calculate the 1st line that is not a whitespace.
            if (assistAssign.isValid(ps.getSelLength(), commandLine, offset)) {
                int commandLineOffset = viewer.getCommandLineOffset();
                try {
                    IDocument doc = sourceViewer.getDocument();
                    while (true) {
                        if (commandLineOffset == doc.getLength() - 1) {
                            break;
                        }
                        char c = doc.getChar(commandLineOffset);
                        if (Character.isWhitespace(c)) {
                            commandLineOffset++;
                        } else {
                            break;
                        }
                    }
                    props.addAll(assistAssign.getProps(ps, SharedUiPlugin.getImageCache(), sourceViewer, offset,
                            commandLine, commandLineOffset));

                } catch (BadLocationException e) {
                    Log.log(e);
                }
            }
        }
    }
    return ConvertCompletionProposals
            .convertHandlesToProposals(props.toArray(new ICompletionProposalHandle[props.size()]));
}
 
Example #12
Source File: PydevConsoleQuickAssistProcessor.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
    return true;
}
 
Example #13
Source File: PythonCorrectionProcessor.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
    return true;
}
 
Example #14
Source File: JavaCorrectionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
	if (invocationContext instanceof IInvocationContext)
		return hasAssists((IInvocationContext)invocationContext);
	return false;
}
 
Example #15
Source File: WordQuickFixProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public IJavaCompletionProposal[] getCorrections(IInvocationContext invocationContext, IProblemLocation[] locations) throws CoreException {

		final int threshold= PreferenceConstants.getPreferenceStore().getInt(PreferenceConstants.SPELLING_PROPOSAL_THRESHOLD);

		int size= 0;
		List<RankedWordProposal> proposals= null;
		String[] arguments= null;

		IProblemLocation location= null;
		RankedWordProposal proposal= null;
		IJavaCompletionProposal[] result= null;

		boolean fixed= false;
		boolean match= false;
		boolean sentence= false;

		final ISpellCheckEngine engine= SpellCheckEngine.getInstance();
		final ISpellChecker checker= engine.getSpellChecker();

		if (checker != null) {

			for (int index= 0; index < locations.length; index++) {
				location= locations[index];
				
				ISourceViewer sourceViewer= null;
				if (invocationContext instanceof IQuickAssistInvocationContext)
					sourceViewer= ((IQuickAssistInvocationContext)invocationContext).getSourceViewer();
				IQuickAssistInvocationContext context= new TextInvocationContext(sourceViewer, location.getOffset(), location.getLength());
				
				if (location.getProblemId() == JavaSpellingReconcileStrategy.SPELLING_PROBLEM_ID) {

					arguments= location.getProblemArguments();
					if (arguments != null && arguments.length > 4) {

						sentence= Boolean.valueOf(arguments[3]).booleanValue();
						match= Boolean.valueOf(arguments[4]).booleanValue();
						fixed= arguments[0].charAt(0) == IHtmlTagConstants.HTML_TAG_PREFIX || arguments[0].charAt(0) == IJavaDocTagConstants.JAVADOC_TAG_PREFIX;

						if ((sentence && match) && !fixed)
							result= new IJavaCompletionProposal[] { new ChangeCaseProposal(arguments, location.getOffset(), location.getLength(), context, engine.getLocale())};
						else {

							proposals= new ArrayList<RankedWordProposal>(checker.getProposals(arguments[0], sentence));
							size= proposals.size();

							if (threshold > 0 && size > threshold) {

								Collections.sort(proposals);
								proposals= proposals.subList(size - threshold - 1, size - 1);
								size= proposals.size();
							}

							boolean extendable= !fixed ? (checker.acceptsWords() || AddWordProposal.canAskToConfigure()) : false;
							result= new IJavaCompletionProposal[size + (extendable ? 3 : 2)];

							for (index= 0; index < size; index++) {

								proposal= proposals.get(index);
								result[index]= new WordCorrectionProposal(proposal.getText(), arguments, location.getOffset(), location.getLength(), context, proposal.getRank());
							}

							if (extendable)
								result[index++]= new AddWordProposal(arguments[0], context);

							result[index++]= new WordIgnoreProposal(arguments[0], context);
							result[index++]= new DisableSpellCheckingProposal(context);
						}
						break;
					}
				}
			}
		}
		return result;
	}
 
Example #16
Source File: PropertiesCorrectionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
	return PropertiesQuickAssistProcessor.hasAssists(createAssistContext(invocationContext));
}
 
Example #17
Source File: AbstractQuickAssistProcessor.java    From google-cloud-eclipse with Apache License 2.0 4 votes vote down vote up
@Override
public ICompletionProposal[] computeQuickAssistProposals(
    IQuickAssistInvocationContext invocationContext) {
  return new ICompletionProposal[] {fix};
}
 
Example #18
Source File: JsonQuickAssistProcessor.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
    return true;
}
 
Example #19
Source File: XtextQuickAssistProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
	return false;
}
 
Example #20
Source File: AnnotationWithQuickFixesHover.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public CompletionProposalRunnable(IQuickAssistInvocationContext invocationContext) {
	this.invocationContext = invocationContext;
}
 
Example #21
Source File: AbstractQuickAssistProcessor.java    From google-cloud-eclipse with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
  return false;
}
 
Example #22
Source File: ChangeCaseProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new change case proposal.
 * 
 * @param arguments The problem arguments associated with the spelling problem
 * @param offset The offset in the document where to apply the proposal
 * @param length The length in the document to apply the proposal
 * @param context The invocation context for this proposal
 * @param locale The locale to use for the case change
 */
public ChangeCaseProposal(final String[] arguments, final int offset, final int length, final IQuickAssistInvocationContext context, final Locale locale) {
	super(Character.isLowerCase(arguments[0].charAt(0)) ? Character.toUpperCase(arguments[0].charAt(0)) + arguments[0].substring(1) : arguments[0], arguments, offset, length, context, Integer.MAX_VALUE);
}
 
Example #23
Source File: DisableSpellCheckingProposal.java    From xds-ide with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new proposal.
 *
 * @param context the invocation context
 */
public DisableSpellCheckingProposal(IQuickAssistInvocationContext context) {
	fContext= context;
}
 
Example #24
Source File: WordIgnoreProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new spell ignore proposal.
 *
 * @param word
 *                   The word to ignore
 * @param context
 *                   The invocation context
 */
public WordIgnoreProposal(final String word, final IQuickAssistInvocationContext context) {
	fWord= word;
	fContext= context;
}
 
Example #25
Source File: AddWordProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new add word proposal
 *
 * @param word
 *                   The word to add
 * @param context
 *                   The invocation context
 */
public AddWordProposal(final String word, final IQuickAssistInvocationContext context) {
	fContext= context;
	fWord= word;
}
 
Example #26
Source File: DisableSpellCheckingProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new proposal.
 *
 * @param context the invocation context
 */
public DisableSpellCheckingProposal(IQuickAssistInvocationContext context) {
	fContext= context;
}
 
Example #27
Source File: AddWordProposal.java    From xds-ide with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new add word proposal
 *
 * @param word
 *                   The word to add
 * @param context
 *                   The invocation context
 */
public AddWordProposal(final String word, final IQuickAssistInvocationContext context) {
	fContext= context;
	fWord= word;
}
 
Example #28
Source File: WordIgnoreProposal.java    From xds-ide with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new spell ignore proposal.
 *
 * @param word
 *                   The word to ignore
 * @param context
 *                   The invocation context
 */
public WordIgnoreProposal(final String word, final IQuickAssistInvocationContext context) {
	fWord= word;
	fContext= context;
}
 
Example #29
Source File: ChangeCaseProposal.java    From xds-ide with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new change case proposal.
 * 
 * @param arguments The problem arguments associated with the spelling problem
 * @param offset The offset in the document where to apply the proposal
 * @param length The length in the document to apply the proposal
 * @param context The invocation context for this proposal
 * @param locale The locale to use for the case change
 */
public ChangeCaseProposal(final String[] arguments, final int offset, final int length, final IQuickAssistInvocationContext context, final Locale locale) {
	super(Character.isLowerCase(arguments[0].charAt(0)) ? Character.toUpperCase(arguments[0].charAt(0)) + arguments[0].substring(1) : arguments[0], arguments, offset, length, context, Integer.MAX_VALUE);
}