| /*=============================================================================# |
| # Copyright (c) 2007, 2019 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.redocs.wikitext.r.ui.sourceediting; |
| |
| |
| import java.util.List; |
| |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.jface.text.BadLocationException; |
| import org.eclipse.jface.text.IDocument; |
| import org.eclipse.jface.text.templates.DocumentTemplateContext; |
| import org.eclipse.jface.text.templates.Template; |
| import org.eclipse.jface.text.templates.TemplateContextType; |
| import org.eclipse.swt.graphics.Image; |
| |
| import org.eclipse.statet.jcommons.text.core.TextRegion; |
| |
| import org.eclipse.statet.docmlet.wikitext.core.markup.IMarkupLanguage; |
| import org.eclipse.statet.docmlet.wikitext.core.source.MarkupLanguageDocumentSetupParticipant; |
| import org.eclipse.statet.internal.redocs.wikitext.r.RedocsWikitextRPlugin; |
| import org.eclipse.statet.ltk.ui.sourceediting.ISourceEditor; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.AssistInvocationContext; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.AssistProposalCollector; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.ContentAssist; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.TemplateCompletionComputer; |
| import org.eclipse.statet.ltk.ui.sourceediting.assist.TemplateProposal; |
| import org.eclipse.statet.redocs.r.ui.RedocsRUIResources; |
| import org.eclipse.statet.redocs.wikitext.r.core.source.IRweaveMarkupLanguage; |
| import org.eclipse.statet.redocs.wikitext.r.core.source.WikitextRweaveTemplateContextType; |
| |
| |
| public class RChunkTemplateCompletionComputer extends TemplateCompletionComputer { |
| |
| |
| private IRweaveMarkupLanguage markupLanguage; |
| |
| |
| public RChunkTemplateCompletionComputer() { |
| super( RedocsWikitextRPlugin.getInstance().getCodegenTemplateStore(), |
| RedocsWikitextRPlugin.getInstance().getCodegenTemplateContextTypeRegistry() ); |
| } |
| |
| |
| // sessionStarted not called automatically, because computer is not registered in registry |
| @Override |
| public void sessionStarted(final ISourceEditor editor, final ContentAssist assist) { |
| final IMarkupLanguage markupLanguage= MarkupLanguageDocumentSetupParticipant.getMarkupLanguage( |
| editor.getViewer().getDocument(), editor.getDocumentContentInfo().getPartitioning() ); |
| if (markupLanguage instanceof IRweaveMarkupLanguage) { |
| this.markupLanguage= (IRweaveMarkupLanguage) markupLanguage; |
| } |
| } |
| |
| @Override |
| public void sessionEnded() { |
| this.markupLanguage= null; |
| } |
| |
| |
| @Override |
| public IStatus computeCompletionProposals(final AssistInvocationContext context, int mode, |
| final AssistProposalCollector proposals, final IProgressMonitor monitor) { |
| // Set to specific mode to force to include templates in default mode |
| if (mode == COMBINED_MODE) { |
| mode= SPECIFIC_MODE; |
| } |
| |
| sessionStarted(context.getEditor(), null); |
| try { |
| if (this.markupLanguage == null) { |
| return null; |
| } |
| return super.computeCompletionProposals(context, mode, proposals, monitor); |
| } |
| finally { |
| sessionEnded(); |
| } |
| } |
| |
| @Override |
| protected boolean include(final Template template, final String prefix) { |
| final String pattern= template.getPattern(); |
| final int varIdx= pattern.indexOf("${"); //$NON-NLS-1$ |
| return ((varIdx < 0 || varIdx >= prefix.length()) && pattern.startsWith(prefix)); |
| } |
| |
| @Override |
| protected String extractPrefix(final AssistInvocationContext context) { |
| final IDocument document= context.getSourceViewer().getDocument(); |
| final int offset= context.getOffset(); |
| try { |
| final int lineOffset= document.getLineOffset(document.getLineOfOffset(offset)); |
| String prefix= document.get(lineOffset, offset - lineOffset); |
| final List<String> indentPrefixes= this.markupLanguage.getIndentPrefixes(); |
| ITER_PREFIX: while (!prefix.isEmpty()) { |
| for (final String indentPrefix : indentPrefixes) { |
| if (prefix.startsWith(indentPrefix)) { |
| prefix= prefix.substring(indentPrefix.length()); |
| continue ITER_PREFIX; |
| } |
| } |
| return prefix; |
| } |
| return ""; //$NON-NLS-1$ |
| } |
| catch (final BadLocationException e) { |
| } |
| return null; |
| } |
| |
| @Override |
| protected TemplateContextType getContextType(final AssistInvocationContext context, |
| final TextRegion region) { |
| return (this.markupLanguage != null) ? |
| getTypeRegistry().getContextType(this.markupLanguage.getName() + |
| WikitextRweaveTemplateContextType.WEAVE_DOCDEFAULT_CONTEXTTYPE_SUFFIX) : |
| null; |
| } |
| |
| @Override |
| protected Image getImage(final Template template) { |
| return RedocsRUIResources.INSTANCE.getImage(RedocsRUIResources.OBJ_RCHUNK_IMAGE_ID); |
| } |
| |
| @Override |
| protected TemplateProposal createProposal(final Template template, |
| final DocumentTemplateContext context, final String prefix, final TextRegion region, |
| int relevance) { |
| relevance= (!prefix.isEmpty() && template.getPattern().startsWith(prefix)) ? |
| 95 : 0; |
| return super.createProposal(template, context, prefix, region, relevance); |
| } |
| |
| } |