下面列出了怎么用org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @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;
}
}
}
}
@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;
}
/**
* 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();
}
@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()]);
}
/**
* 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();
}
/**
* @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;
}
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);
}
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;
}
@Override
public ICompletionProposal[] computeQuickAssistProposals(
IQuickAssistInvocationContext invocationContext) {
return new ICompletionProposal[] {fix};
}
@Override
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
return false;
}
public CompletionProposalRunnable(IQuickAssistInvocationContext invocationContext) {
this.invocationContext = invocationContext;
}
@Override
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
return false;
}
@Override
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
return true;
}
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;
}
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
return PropertiesQuickAssistProcessor.hasAssists(createAssistContext(invocationContext));
}
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;
}
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
if (invocationContext instanceof IInvocationContext)
return hasAssists((IInvocationContext)invocationContext);
return false;
}
@Override
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
return true;
}
@Override
public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
return true;
}
/**
* 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()]));
}
public IQuickAssistInvocationContext getQuickAssistInvocationContext() {
return viewer.getQuickAssistInvocationContext();
}
/**
* 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);
}
/**
* 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;
}
/**
* 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;
}
/**
* Creates a new proposal.
*
* @param context the invocation context
*/
public DisableSpellCheckingProposal(IQuickAssistInvocationContext context) {
fContext= context;
}
/**
* 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);
}
/**
* 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;
}
/**
* 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;
}
/**
* Creates a new proposal.
*
* @param context the invocation context
*/
public DisableSpellCheckingProposal(IQuickAssistInvocationContext context) {
fContext= context;
}