// FIXME: assumes C-family multi-line comments. private void insertMatchingCloseComment() { final int position = textArea.getSelectionStart(); String whitespace = getIndentationOfLineAtOffset(position); String prefix = "\n" + whitespace + " * "; String suffix = "\n" + whitespace + " */"; textArea.replaceSelection(prefix + suffix); final int newOffset = position + prefix.length(); textArea.select(newOffset, newOffset); }
private boolean insertMatchingBrackets() { final int start = textArea.getSelectionStart(); final int end = textArea.getSelectionEnd(); int endLineIndex = textArea.getLineOfOffset(end); int suffixPosition = textArea.getLineEndOffsetBeforeTerminator(endLineIndex); String beforeInsertion = textArea.getTextBuffer().subSequence(0, start).toString(); String afterInsertion = textArea .getTextBuffer() .subSequence(suffixPosition, textArea.getTextBuffer().length()) .toString(); String unmatchedOpenBrackets = getUnmatchedBrackets(beforeInsertion); String unmatchedCloseBrackets = getUnmatchedBrackets(afterInsertion); String reflectedCloseBrackets = PBracketUtilities.reflectBrackets(unmatchedCloseBrackets); if (unmatchedOpenBrackets.startsWith(reflectedCloseBrackets) == false) { return false; } String closingBrackets = PBracketUtilities.reflectBrackets( unmatchedOpenBrackets.substring(reflectedCloseBrackets.length())); if (closingBrackets.length() == 0) { return false; } String startLine = getLineTextAtOffset(start); if (closingBrackets.endsWith("}") == false || textArea.getIndenter().isInNeedOfClosingSemicolon(startLine)) { // TODO: "closingBrackets" is a bad name now it can have a semicolon on the end! closingBrackets = closingBrackets + ";"; } String candidateBlockContents = textArea.getTextBuffer().subSequence(end, suffixPosition).toString(); String commonEnding = getCommonEnding(candidateBlockContents, closingBrackets); String whitespace = getIndentationOfLineAtOffset(start); // TODO: The newline inserter has no business thinking it knows how to increase the indent. String prefix = "\n" + whitespace + textArea.getIndentationString(); String suffix = "\n" + whitespace + closingBrackets; final int newCaretPosition = start + prefix.length(); textArea.replaceSelection(prefix); // suffixPosition is invalidated by replaceSelection. // But we can't swap the calls because replaceRange clears the selection. int selectionSize = end - start; suffixPosition -= selectionSize; suffixPosition += prefix.length(); textArea.replaceRange(suffix, suffixPosition - commonEnding.length(), suffixPosition); textArea.select(newCaretPosition, newCaretPosition); return true; }