private boolean isReplacedAreaEmpty(TemplateContext context) { // don't trim the buffer if the replacement area is empty // case: surrounding empty lines with block if (context instanceof DocumentTemplateContext) { DocumentTemplateContext dtc= (DocumentTemplateContext) context; if (dtc.getStart() == dtc.getCompletionOffset()) try { if (dtc.getDocument().get(dtc.getStart(), dtc.getEnd() - dtc.getStart()).trim().length() == 0) return true; } catch (BadLocationException x) { // ignore - this may happen when the document was modified after the initial invocation, and the // context does not track the changes properly - don't trim in that case return true; } } return false; }
public SwaggerTemplateContext(DocumentTemplateContext context) { super(context.getContextType(), context.getDocument(), context.getCompletionOffset(), context .getCompletionLength()); }
/** * Returns the keyword which triggered template insertion. * * @return the keyword which triggered template insertion */ public String getKey() { int offset= getStart(); int length= getEnd() - offset; try { return fDocument.get(offset, length); } catch (BadLocationException e) { return ""; //$NON-NLS-1$ } }
/** * Returns the offset of the range in the document that will be replaced by * applying this template. * * @return the offset of the range in the document that will be replaced by * applying this template * @since 3.1 */ protected final int getReplaceOffset() { int start; if (fContext instanceof DocumentTemplateContext) { DocumentTemplateContext docContext = (DocumentTemplateContext)fContext; start= docContext.getStart(); } else { start= fRegion.getOffset(); } return start; }
/** * Returns the end offset of the range in the document that will be replaced * by applying this template. * * @return the end offset of the range in the document that will be replaced * by applying this template * @since 3.1 */ protected final int getReplaceEndOffset() { int end; if (fContext instanceof DocumentTemplateContext) { DocumentTemplateContext docContext = (DocumentTemplateContext)fContext; end= docContext.getEnd(); } else { end= fRegion.getOffset() + fRegion.getLength(); } return end; }
@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { if (!canEvaluate(template)) return null; TemplateTranslator translator= new TemplateTranslator(); TemplateBuffer buffer= translator.translate(template); getContextType().resolve(buffer, this); return buffer; } }
/** * Check whether the template is allowed eventhough the context can't evaluate it. This is * needed because the Dropping of a template is more lenient than ctl-space invoked code assist. * * @param context the template context * @param template the template * @return true if the template is allowed */ private boolean isTemplateAllowed(DocumentTemplateContext context, Template template) { int offset; try { if (template.getContextTypeId().equals(JavaDocContextType.ID)) { return (offset= context.getCompletionOffset()) > 0 && Character.isWhitespace(context.getDocument().getChar(offset - 1)); } else { return ((offset= context.getCompletionOffset()) > 0 && !isTemplateNamePart(context.getDocument().getChar(offset - 1))); } } catch (BadLocationException e) { } return false; }
@Override protected boolean isValidTemplate(IDocument document, Template template, int offset, int length) { String[] contextIds= getContextTypeIds(document, offset); for (int i= 0; i < contextIds.length; i++) { if (contextIds[i].equals(template.getContextTypeId())) { DocumentTemplateContext context= getContext(document, template, offset, length); return context.canEvaluate(template) || isTemplateAllowed(context, template); } } return false; }
/** * Returns <code>true</code> if the proposal has a selection, e.g. will wrap some code. * * @return <code>true</code> if the proposals completion length is non zero * @since 3.2 */ private boolean isSelectionTemplate() { if (fContext instanceof DocumentTemplateContext) { DocumentTemplateContext ctx= (DocumentTemplateContext) fContext; if (ctx.getCompletionLength() > 0) return true; } return false; } }
if (fContext instanceof DocumentTemplateContext) { DocumentTemplateContext templateContext= (DocumentTemplateContext) fContext; IDocument document= templateContext.getDocument();
/** * Creates a concrete template context for the given region in the document. This involves finding out which * context type is valid at the given location, and then creating a context of this type. The default implementation * returns a <code>DocumentTemplateContext</code> for the context type at the given location. * * @param viewer the viewer for which the context is created * @param region the region into <code>document</code> for which the context is created * @return a template context that can handle template insertion at the given location, or <code>null</code> */ protected TemplateContext createContext(ITextViewer viewer, IRegion region) { TemplateContextType contextType= getContextType(viewer, region); if (contextType != null) { IDocument document= viewer.getDocument(); return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength()); } return null; }
/** * Returns the keyword which triggered template insertion. * * @return the keyword which triggered template insertion */ public String getKey() { int offset= getStart(); int length= getEnd() - offset; try { return fDocument.get(offset, length); } catch (BadLocationException e) { return ""; //$NON-NLS-1$ } }
/** * Returns the offset of the range in the document that will be replaced by * applying this template. * * @return the offset of the range in the document that will be replaced by * applying this template * @since 3.1 */ protected final int getReplaceOffset() { int start; if (fContext instanceof DocumentTemplateContext) { DocumentTemplateContext docContext = (DocumentTemplateContext)fContext; start= docContext.getStart(); } else { start= fRegion.getOffset(); } return start; }
/** * Returns the end offset of the range in the document that will be replaced * by applying this template. * * @return the end offset of the range in the document that will be replaced * by applying this template * @since 3.1 */ protected final int getReplaceEndOffset() { int end; if (fContext instanceof DocumentTemplateContext) { DocumentTemplateContext docContext = (DocumentTemplateContext)fContext; end= docContext.getEnd(); } else { end= fRegion.getOffset() + fRegion.getLength(); } return end; }
@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { if (!canEvaluate(template)) return null; TemplateTranslator translator= new TemplateTranslator(); TemplateBuffer buffer= translator.translate(template); getContextType().resolve(buffer, this); return buffer; } }
/** * Check whether the template is allowed eventhough the context can't evaluate it. This is * needed because the Dropping of a template is more lenient than ctl-space invoked code assist. * * @param context the template context * @param template the template * @return true if the template is allowed */ private boolean isTemplateAllowed(DocumentTemplateContext context, Template template) { int offset; try { if (template.getContextTypeId().equals(JavaDocContextType.ID)) { return (offset= context.getCompletionOffset()) > 0 && Character.isWhitespace(context.getDocument().getChar(offset - 1)); } else { return ((offset= context.getCompletionOffset()) > 0 && !isTemplateNamePart(context.getDocument().getChar(offset - 1))); } } catch (BadLocationException e) { } return false; }
@Override protected boolean isValidTemplate(IDocument document, Template template, int offset, int length) { String[] contextIds= getContextTypeIds(document, offset); for (int i= 0; i < contextIds.length; i++) { if (contextIds[i].equals(template.getContextTypeId())) { DocumentTemplateContext context= getContext(document, template, offset, length); return context.canEvaluate(template) || isTemplateAllowed(context, template); } } return false; }
/** * Returns <code>true</code> if the proposal has a selection, e.g. will wrap some code. * * @return <code>true</code> if the proposals completion length is non zero * @since 3.2 */ private boolean isSelectionTemplate() { if (fContext instanceof DocumentTemplateContext) { DocumentTemplateContext ctx= (DocumentTemplateContext) fContext; if (ctx.getCompletionLength() > 0) return true; } return false; } }
if (fContext instanceof DocumentTemplateContext) { DocumentTemplateContext templateContext= (DocumentTemplateContext) fContext; IDocument document= templateContext.getDocument();
/** * Creates a concrete template context for the given region in the document. This involves finding out which * context type is valid at the given location, and then creating a context of this type. The default implementation * returns a <code>DocumentTemplateContext</code> for the context type at the given location. * * @param viewer the viewer for which the context is created * @param region the region into <code>document</code> for which the context is created * @return a template context that can handle template insertion at the given location, or <code>null</code> */ protected TemplateContext createContext(ITextViewer viewer, IRegion region) { TemplateContextType contextType= getContextType(viewer, region); if (contextType != null) { IDocument document= viewer.getDocument(); return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength()); } return null; }