@Override public void customizeDocumentCommand(IDocument document, DocumentCommand command) { if (command.text.equals("{")) { int line; try { line = document.getLineOffset(command.offset); String indent = getIndentOfLine(document, line); command.text = "{" + "\r\n" + indent + "}"; configureCommand(command); } catch (BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
protected void addClosingBracketAfterEnterIfRequired(IDocument document, DocumentCommand command, String text, String textBefore, String textAfter) { boolean isLineBreak = isLineBreak(text); if (!isLineBreak) { return; } String documentText = document.get(); String openingBracketBefore = getCloseAfterBracketBefore(documentText, command.offset); if (openingBracketBefore == null) { return; } // add additional indentation (because a line break was entered after an opening // bracket) command.text = command.text + "\t"; String closingBracket = bracketSet.getCounterpart(openingBracketBefore); boolean closingBracketIsMissing = count(documentText, openingBracketBefore) != count(documentText, closingBracket); // add closing bracket (if required) if (closingBracketIsMissing) { command.text = command.text + textAfter; command.text = command.text + closingBracket; } command.shiftsCaret = false; command.caretOffset = command.offset + textAfter.length() + 1; }
/** * Returns a text String of the (line + <code>lineDif</code>). * @param document IDocument that contains the line. * @param command DocumentCommand that determines the line. * @param delim are delimiters included * @param lineDif 0 = current line, 1 = next line, -1 previous line etc... * @return the text of the line. */ public String getStringAt(IDocument document, DocumentCommand command, boolean delim, int lineDif) { String line = ""; int lineBegin, lineLength; try { if (delim) { lineLength = getLineLength(document, command, true, lineDif); } else { lineLength = getLineLength(document, command, false, lineDif); } if (lineLength > 0) { lineBegin = document.getLineOffset(document .getLineOfOffset(command.offset) + lineDif); line = document.get(lineBegin, lineLength); } } catch (BadLocationException e) { TexlipsePlugin.log("TexEditorTools.getStringAt", e); } return line; }
/** * Erases the \item -string from a line * * @param d * @param c */ private void dropItem(IDocument d, DocumentCommand c) { try { if (itemSetted && itemAtLine == (d.getLineOfOffset(c.offset) - 1)) { IRegion r = d.getLineInformationOfOffset(c.offset); String line = d.get(r.getOffset(), r.getLength()); if ("\\item".equals(line.trim()) || "\\item[]".equals(line.trim())) { c.shiftsCaret = false; c.length = line.length(); c.offset = r.getOffset(); c.text = getIndentation(line); c.caretOffset = c.offset + c.text.length(); } } } catch (BadLocationException e) { TexlipsePlugin.log("TexAutoIndentStrategy:dropItem", e); } itemSetted = false; }
@Override public void customizeDocumentCommand(IDocument document, DocumentCommand command) { if (command.text.equalsIgnoreCase("\"")) { command.text = "\"\""; configureCommand(command); } else if (command.text.equalsIgnoreCase("'")) { command.text = "''"; configureCommand(command); } else if (command.text.equalsIgnoreCase("[")) { command.text = "[]"; configureCommand(command); } else if (command.text.equalsIgnoreCase("(")) { command.text = "()"; configureCommand(command); } }
@Override public void customizeDocumentCommand(IDocument document, DocumentCommand command) { if (!supportsSmartInsert(document)) { return; } IStructuredModel model = null; try { model = StructuredModelManager.getModelManager().getExistingModelForRead(document); if (model != null) { if (command.text != null) { smartInsertCloseEndEL(command, document, model); } } } finally { if (model != null) model.releaseFromRead(); } }
private void smartInsertCloseEndEL(DocumentCommand command, IDocument document, IStructuredModel model) { try { if (isPreferenceEnabled(AngularUIPreferenceNames.TYPING_COMPLETE_END_EL) && command.text.equals("{") //$NON-NLS-1$ && document.getLength() > 0 && document.getChar(command.offset - 1) == '{') { IDOMNode node = (IDOMNode) model.getIndexedRegion(command.offset - 1); command.text += "}}"; command.shiftsCaret = false; command.caretOffset = command.offset + 1; command.doit = false; } } catch (BadLocationException e) { } }
@Override public void customizeDocumentCommand(final IDocument doc, final DocumentCommand cmd) { if (cmd.length != 0 || cmd.text == null) return; try { if (TextUtilities.endsWith(doc.getLegalLineDelimiters(), cmd.text) != -1) { if (AutoEdit.isBlankLine(doc, cmd.offset)) { int beg = AutoEdit.getLineOffset(doc, cmd.offset); int len = cmd.offset - beg; DeleteEdit blanks = new DeleteEdit(beg, len); blanks.apply(doc); cmd.offset = beg; } else { autoIndent(doc, cmd, false); // return entered } } else if (evaluateInsertPoint(doc, cmd)) { autoIndent(doc, cmd, true); // insert point exceeds limit } else if (evaluateLineWidth(doc, cmd)) { wrapLines(doc, cmd); // line end exceeds limit } } catch (BadLocationException e) {} }
private String createMethodTags(IDocument document, DocumentCommand command, String indentation, String lineDelimiter, IFunction method) throws CoreException, BadLocationException { IRegion partition = TextUtilities.getPartition(document, fPartitioning, command.offset, false); IFunction inheritedMethod = getInheritedMethod(method); String comment = CodeGeneration.getMethodComment(method, inheritedMethod, lineDelimiter); if (comment != null) { comment = comment.trim(); boolean javadocComment = comment.startsWith("/**"); //$NON-NLS-1$ if (!isFirstComment(document, command, method, javadocComment)) return null; boolean isJavaDoc = partition.getLength() >= 3 && document.get(partition.getOffset(), 3).equals("/**"); //$NON-NLS-1$ if (javadocComment == isJavaDoc) { return prepareTemplateComment(comment, indentation, method.getJavaScriptProject(), lineDelimiter); } } return null; }
public void customizeDocumentCommand(IDocument document, DocumentCommand command) { if (!isSmartMode()) return; if (command.text != null) { if (command.length == 0) { String[] lineDelimiters = document.getLegalLineDelimiters(); int index = TextUtilities.endsWith(lineDelimiters, command.text); if (index > -1) { // ends with line delimiter if (lineDelimiters[index].equals(command.text)) // just the line delimiter indentAfterNewLine(document, command); return; } } if (command.text.equals("/")) { //$NON-NLS-1$ indentAfterCommentEnd(document, command); return; } } }
/** * Expects the cursor to be in the same line as the start terminal * puts any text between start terminal and cursor into a separate newline before the cursor. * puts any text between cursor and end terminal into a separate newline after the cursor. * puts the closing terminal into a separate line at the end. * adds a closing terminal if not existent. */ protected CommandInfo handleCursorInFirstLine(IDocument document, DocumentCommand command, IRegion startTerminal, IRegion stopTerminal) throws BadLocationException { CommandInfo newC = new CommandInfo(); newC.isChange = true; newC.offset = command.offset; newC.text += command.text + indentationString; newC.cursorOffset = command.offset + newC.text.length(); if (stopTerminal == null && atEndOfLineInput(document, command.offset)) { newC.text += command.text + getRightTerminal(); } if (stopTerminal != null && stopTerminal.getOffset() >= command.offset && util.isSameLine(document, stopTerminal.getOffset(), command.offset)) { String string = document.get(command.offset, stopTerminal.getOffset() - command.offset); if (string.trim().length() > 0) newC.text += string.trim(); newC.text += command.text; newC.length += string.length(); } return newC; }
@Override protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command) throws BadLocationException { if (command.text.equals("") && command.length == 1) { if (command.offset + right.length() + left.length() > document.getLength()) return; if (command.offset + command.length - left.length() < 0) return; if (command.length != left.length()) return; String string = document.get(command.offset, left.length() + right.length()); if (string.equals(left + right)) { command.length = left.length() + right.length(); } } }
@Override protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command) throws BadLocationException { if (command.length != 0) return; String[] lineDelimiters = document.getLegalLineDelimiters(); int delimiterIndex = TextUtilities.startsWith(lineDelimiters, command.text); if (delimiterIndex != -1) { MultiLineTerminalsEditStrategy bestStrategy = null; IRegion bestStart = null; for(MultiLineTerminalsEditStrategy strategy: strategies) { IRegion candidate = strategy.findStartTerminal(document, command.offset); if (candidate != null) { if (bestStart == null || bestStart.getOffset() < candidate.getOffset()) { bestStrategy = strategy; bestStart = candidate; } } } if (bestStrategy != null) { bestStrategy.internalCustomizeDocumentCommand(document, command); } } }
private boolean checkAndPrepareForNewline(IDocument document, DocumentCommand command) { try { String documentAndCommandText = document.get(0, command.offset) + command.text; /* * The only case where we want to add an indentation after newline is if * there is a opening curly brace. */ Matcher matcher = NEWLINE_AFTER_OPEN_BRACE.matcher(documentAndCommandText); if (!matcher.matches()) { return false; } } catch (BadLocationException e) { return false; } /* * The StructuredAutoEditStrategyCSS will only add proper indentation if the * last character is a newline. We remove the whitespace following the * newline which was added by the XML autoedit strategy that comes before * us. */ command.text = command.text.replaceAll("\\n\\s+$", "\n"); return command.text.endsWith("\n"); }
public void smartIndentOnBackspace(IDocument doc, DocumentCommand cmd) { if (isUsingTab) { return; } if ((cmd.offset == -1) || (doc.getLength() == 0)) { return; } try { int len = doc.getLength(); int p = (cmd.offset != len) ? cmd.offset : (cmd.offset - 1); int line = doc.getLineOfOffset(p); int start = doc.getLineOffset(line); int whiteend = findEndOfWhiteSpace(doc, start, start + doc.getLineLength(line)); if ((cmd.offset + 1) != whiteend) { return; } int indent = indentWidthOf(doc.get(start, whiteend - start), fTabWidth); indent = unIndent(indent, 1); cmd.offset = start; cmd.length = whiteend - start; cmd.text = getIndentString(indent).toString(); } catch (BadLocationException e) { } }
public void sameIndentAs(int matchoffset, int line, IDocument document, DocumentCommand cmd) { try { int indent = getIndentOfLine(document, document.getLineOfOffset(matchoffset)); StringBuffer buf = getIndentString(indent); int start = (line < 0) ? 0 : document.getLineOffset(line); int whiteend = findEndOfWhiteSpace(document, start, cmd.offset); buf.append(document.get(whiteend, cmd.offset - whiteend)); buf.append(cmd.text); cmd.length += (cmd.offset - start); cmd.offset = start; cmd.text = buf.toString(); } catch (BadLocationException e) { } }
private ICompletionProposal escape(DocumentCommand command) { try { String charsetName= fFile.getCharset(); if (!charsetName.equals(fCharsetName)) { fCharsetName= charsetName; fCharsetEncoder= Charset.forName(fCharsetName).newEncoder(); } } catch (CoreException e) { return null; } String text= command.text; boolean escapeUnicodeChars= !fCharsetEncoder.canEncode(text); boolean escapeBackslash= (text.length() > 1) && ((escapeUnicodeChars && PropertiesFileEscapes.containsUnescapedBackslash(text)) || PropertiesFileEscapes.containsInvalidEscapeSequence(text)); if (!escapeUnicodeChars && !escapeBackslash) return null; command.text= PropertiesFileEscapes.escape(text, false, false, escapeUnicodeChars); if (escapeBackslash) { String proposalText= PropertiesFileEscapes.escape(text, false, true, escapeUnicodeChars); return new EscapeBackslashCompletionProposal(proposalText, command.offset, command.text.length(), PropertiesFileEditorMessages.EscapeBackslashCompletionProposal_escapeBackslashesInOriginalString); } return null; }
private void handleSmartTrigger(IDocument document, char trigger, int referenceOffset) throws BadLocationException { DocumentCommand cmd= new DocumentCommand() { }; cmd.offset= referenceOffset; cmd.length= 0; cmd.text= Character.toString(trigger); cmd.doit= true; cmd.shiftsCaret= true; cmd.caretOffset= getReplacementOffset() + getCursorPosition(); SmartSemicolonAutoEditStrategy strategy= new SmartSemicolonAutoEditStrategy(IJavaPartitions.JAVA_PARTITIONING); strategy.customizeDocumentCommand(document, cmd); replace(document, cmd.offset, cmd.length, cmd.text); setCursorPosition(cmd.caretOffset - getReplacementOffset() + cmd.text.length()); }
/** * Creates the Javadoc tags for newly inserted comments. * * @param document the document * @param command the command * @param indentation the base indentation to use * @param lineDelimiter the line delimiter to use * @param unit the compilation unit shown in the editor * @return the tags to add to the document * @throws CoreException if accessing the Java model fails * @throws BadLocationException if accessing the document fails */ private String createJavaDocTags(IDocument document, DocumentCommand command, String indentation, String lineDelimiter, ICompilationUnit unit) throws CoreException, BadLocationException { IJavaElement element= unit.getElementAt(command.offset); if (element == null) return null; switch (element.getElementType()) { case IJavaElement.TYPE: return createTypeTags(document, command, indentation, lineDelimiter, (IType) element); case IJavaElement.METHOD: return createMethodTags(document, command, indentation, lineDelimiter, (IMethod) element); default: return null; } }
private String createMethodTags(IDocument document, DocumentCommand command, String indentation, String lineDelimiter, IMethod method) throws CoreException, BadLocationException { IRegion partition= TextUtilities.getPartition(document, fPartitioning, command.offset, false); IMethod inheritedMethod= getInheritedMethod(method); String comment= CodeGeneration.getMethodComment(method, inheritedMethod, lineDelimiter); if (comment != null) { comment= comment.trim(); boolean javadocComment= comment.startsWith("/**"); //$NON-NLS-1$ if (!isFirstComment(document, command, method, javadocComment)) return null; boolean isJavaDoc= partition.getLength() >= 3 && document.get(partition.getOffset(), 3).equals("/**"); //$NON-NLS-1$ if (javadocComment == isJavaDoc) { return prepareTemplateComment(comment, indentation, method.getJavaProject(), lineDelimiter); } } return null; }
@Override public void customizeDocumentCommand(IDocument document, DocumentCommand command) { if (!isSmartMode()) return; if (command.text != null) { if (command.length == 0) { String[] lineDelimiters= document.getLegalLineDelimiters(); int index= TextUtilities.endsWith(lineDelimiters, command.text); if (index > -1) { // ends with line delimiter if (lineDelimiters[index].equals(command.text)) // just the line delimiter indentAfterNewLine(document, command); return; } } if (command.text.equals("/")) { //$NON-NLS-1$ indentAfterCommentEnd(document, command); return; } } }
public String getPreviousLineIndent(DocumentCommand command, IDocument document) { String prevLineIndent = ""; try { // find start of line int p = (command.offset == document.getLength() ? command.offset - 1 : command.offset); IRegion info = document.getLineInformationOfOffset(p); int start = info.getOffset(); // find white spaces int end = findEndOfWhiteSpace(document, start, command.offset); if (end > start) { prevLineIndent = document.get(start, end - start); } } catch (BadLocationException e) { //ignore } return prevLineIndent; }
private String getReplacement(DocumentCommand command, Tuple<String, String> found, String selectedText) { String replacement; if (found != null && found.o1.equals(command.text)) { replacement = found.o1 + selectedText + found.o2; } else { if (command.text.length() == 1) { char peer; char char0 = command.text.charAt(0); try { peer = StringUtils.getPeer(char0); } catch (Exception e) { peer = char0; } replacement = char0 + selectedText + peer; } else { replacement = command.text + selectedText + command.text; } } return replacement; }
/** * Called right after a ' or " */ public void handleAutoClose(IDocument document, DocumentCommand command, char start, char end) { TextSelectionUtils ps = new TextSelectionUtils(document, new TextSelection(document, command.offset, command.length)); try { char nextChar = ps.getCharAfterCurrentOffset(); if (Character.isJavaIdentifierPart(nextChar)) { //we're just before a word (don't try to do anything in this case) //e.g. |var (| is cursor position) return; } } catch (BadLocationException e) { } command.text = Character.toString(start) + Character.toString(end); command.shiftsCaret = false; command.caretOffset = command.offset + 1; }
public void handleAutoSkip(IDocument document, DocumentCommand command, char c) { TextSelectionUtils ps = new TextSelectionUtils(document, new TextSelection(document, command.offset, command.length)); Tuple<String, String> beforeAndAfterMatchingChars = ps.getBeforeAndAfterMatchingChars(c); //Reminder: int matchesBefore = beforeAndAfterMatchingChars.o1.length(); int matchesAfter = beforeAndAfterMatchingChars.o2.length(); if (matchesAfter == 1) { //just walk the caret command.text = ""; command.shiftsCaret = false; command.caretOffset = command.offset + 1; } }
public void handleCloseParens(IDocument document, DocumentCommand command, char c) { // you can only do the replacement if the next character already there is what the user is trying to input try { if (command.offset < document.getLength() && document.get(command.offset, 1).equals(command.text)) { // the following searches through each of the end braces and // sees if the command has one of them boolean found = false; for (int i = 1; i <= BRACKETS.length && !found; i += 2) { char b = BRACKETS[i]; if (b == c) { found = true; performPairReplacement(document, command); } } } } catch (BadLocationException e) { Log.log(e); } }
/** * @return true if we should skip a ), ] or } */ public boolean canSkipCloseParenthesis(IDocument document, DocumentCommand command) throws BadLocationException { TextSelectionUtils ps = new TextSelectionUtils(document, command.offset); char c = ps.getCharAtCurrentOffset(); try { char peer = StringUtils.getPeer(c); String contentType = getContentType(document, command); String doc = getPartsWithPartition(document, contentType); int chars = StringUtils.countChars(c, doc); int peers = StringUtils.countChars(peer, doc); boolean skipChar = chars == peers; return skipChar; } catch (NoPeerAvailableException e) { return false; } }
/** * Overridden because we want to do things differently in block selection mode. */ @Override protected void customizeDocumentCommand(DocumentCommand command) { if (pyAutoIndentStrategy == null) { pyAutoIndentStrategy = this.getEdit().getAutoEditStrategy(); } boolean blockSelection = false; try { blockSelection = this.getTextWidget().getBlockSelection(); } catch (Throwable e) { //that's OK (only available in eclipse 3.5) } pyAutoIndentStrategy.setBlockSelection(blockSelection); super.customizeDocumentCommand(command); }