From 924edc3948c312fd53f4d500996e0eba8f555669 Mon Sep 17 00:00:00 2001 From: bananeweizen Date: Fri, 27 Jan 2006 22:11:45 +0000 Subject: [PATCH] implemented 1416503, automatic closing of braces --- .../phpeclipse/phpeditor/PHPUnitEditor.java | 133 ++++++++------------ 1 files changed, 52 insertions(+), 81 deletions(-) diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPUnitEditor.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPUnitEditor.java index 153bf6d..41387fe 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPUnitEditor.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPUnitEditor.java @@ -17,6 +17,7 @@ import net.sourceforge.phpdt.core.JavaCore; import net.sourceforge.phpdt.core.JavaModelException; import net.sourceforge.phpdt.core.dom.CompilationUnit; import net.sourceforge.phpdt.internal.compiler.parser.Scanner; +import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility; import net.sourceforge.phpdt.internal.ui.actions.AddBlockCommentAction; import net.sourceforge.phpdt.internal.ui.actions.CompositeActionGroup; import net.sourceforge.phpdt.internal.ui.actions.IndentAction; @@ -914,10 +915,6 @@ public class PHPUnitEditor extends PHPEditor { //implements private boolean fCloseStringsPHPSQ = true; - private boolean fCloseBracketsHTML = true; - - private boolean fCloseStringsHTML = true; - private int fOffset; private int fLength; @@ -934,14 +931,6 @@ public class PHPUnitEditor extends PHPEditor { //implements fCloseStringsPHPSQ = enabled; } - public void setCloseBracketsHTMLEnabled(boolean enabled) { - fCloseBracketsHTML = enabled; - } - - public void setCloseStringsHTMLEnabled(boolean enabled) { - fCloseStringsHTML = enabled; - } - private boolean hasIdentifierToTheRight(IDocument document, int offset) { try { int end = offset; @@ -969,6 +958,19 @@ public class PHPUnitEditor extends PHPEditor { //implements } } + private boolean hasCharacterToTheLeft(IDocument document, int offset, char character) { + try { + int start = offset; + IRegion startLine = document.getLineInformationOfOffset(start); + int minStart = startLine.getOffset(); + while (start != minStart && Character.isWhitespace(document.getChar(start - 1))) + --start; + return start != minStart && document.getChar(start - 1)== character; + } catch (BadLocationException e) { + return false; + } + } + private boolean hasCharacterToTheRight(IDocument document, int offset, char character) { try { int end = offset; @@ -1048,7 +1050,6 @@ public class PHPUnitEditor extends PHPEditor { //implements sourceViewer.setSelectedRange(newSelectionDQ.getOffset(), newSelectionDQ.getLength()); event.doit = false; break; - // fall through case '\'': if (event.character == '\'') { if (!fCloseStringsPHPSQ) @@ -1086,57 +1087,37 @@ public class PHPUnitEditor extends PHPEditor { //implements IRegion newSelectionSQ = editorSQ.getSelectedRegion(); sourceViewer.setSelectedRange(newSelectionSQ.getOffset(), newSelectionSQ.getLength()); event.doit = false; + case '\r': { // insert linebreaks and new closing brace after brace and return + if (!fCloseBracketsPHP) { + return; + } + if (hasCharacterToTheLeft(document,offset,'{')) { + String lineDelimiter=StubUtility.getLineDelimiterFor(document); + int caretPos=sourceViewer.getTextWidget().getCaretOffset(); + final StringBuffer buffer = new StringBuffer(lineDelimiter); + // get indentation + IRegion line = document.getLineInformationOfOffset(offset); + String currentLine=document.get(line.getOffset(),line.getLength()); + int index=0; + int max=currentLine.length(); + StringBuffer indent=new StringBuffer(); + while (index