1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 289461 - Editing a method's comment/documentation block results in wrong line-ending character (LF instead of CRLF) to be inserted

This commit is contained in:
Anton Leherbauer 2010-08-13 09:36:38 +00:00
parent 96c5c7725a
commit 771fd82c2f
2 changed files with 45 additions and 11 deletions

View file

@ -1,12 +1,13 @@
/*******************************************************************************
* Copyright (c) 2008, 2009 Symbian Software Systems and others.
* Copyright (c) 2008, 2010 Symbian Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Andrew Ferguson (Symbian) - Initial implementation
* Andrew Ferguson (Symbian) - Initial implementation
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.ui.text.doctools;
@ -54,6 +55,7 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
protected static final String MULTILINE_START = "/*"; //$NON-NLS-1$#
protected static final String MULTILINE_MID = " * "; //$NON-NLS-1$
protected static final String MULTILINE_END = "*/"; //$NON-NLS-1$
private static String fgDefaultLineDelim = "\n"; //$NON-NLS-1$
public DefaultMultilineCommentAutoEditStrategy() {
}
@ -62,6 +64,7 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
* @see org.eclipse.jface.text.IAutoEditStrategy#customizeDocumentCommand(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.DocumentCommand)
*/
public void customizeDocumentCommand(IDocument doc, DocumentCommand cmd) {
fgDefaultLineDelim = TextUtilities.getDefaultLineDelimiter(doc);
if(doc instanceof IDocumentExtension4) {
boolean forNewLine= cmd.length == 0 && cmd.text != null && endsWithDelimiter(doc, cmd.text);
boolean forCommentEnd= "/".equals(cmd.text); //$NON-NLS-1$
@ -115,6 +118,7 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
if (offset == -1 || doc.getLength() == 0)
return;
String lineDelim = TextUtilities.getDefaultLineDelimiter(doc);
final StringBuilder buf= new StringBuilder(c.text);
try {
// find start of line
@ -139,7 +143,7 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
if(commentAtStart && shouldCloseMultiline(doc, c.offset)) {
try {
doc.replace(c.offset, 0, indentation+" "+MULTILINE_END); // close the comment in order to parse //$NON-NLS-1$
buf.append("\n"); //$NON-NLS-1$
buf.append(lineDelim);
// as we are auto-closing, the comment becomes eligible for auto-doc'ing
IASTDeclaration dec= null;
@ -159,7 +163,7 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
if(dec!=null) {
ITypedRegion partition= TextUtilities.getPartition(doc, ICPartitions.C_PARTITIONING /* this! */, offset, false);
StringBuilder content= customizeAfterNewLineForDeclaration(doc, dec, partition);
buf.append(indent(content, indentation + MULTILINE_MID));
buf.append(indent(content, indentation + MULTILINE_MID, lineDelim));
}
} catch(BadLocationException ble) {
@ -322,13 +326,15 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
* of each line in the specified input buffer
* @param buffer
* @param indent
* @param lineDelim
* @since 5.2
*/
protected static final StringBuilder indent(StringBuilder buffer, String indent) {
protected static final StringBuilder indent(StringBuilder buffer, String indent, String lineDelim) {
StringBuilder result= new StringBuilder();
BufferedReader br= new BufferedReader(new StringReader(buffer.toString()));
try {
for(String line= br.readLine(); line!=null; line= br.readLine()) {
result.append(indent + line + "\n"); //$NON-NLS-1$
result.append(indent).append(line).append(lineDelim);
}
} catch(IOException ioe) {
throw new AssertionError(); // we can't get IO errors from a string backed reader
@ -336,6 +342,19 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
return result;
}
/**
* Returns a new buffer with the specified indent string inserted at the beginning
* of each line in the specified input buffer
* @param buffer
* @param indent
*
* @deprecated Use {{@link #indent(StringBuilder, String, String)} instead.
*/
@Deprecated
protected static final StringBuilder indent(StringBuilder buffer, String indent) {
return indent(buffer, indent, fgDefaultLineDelim);
}
/**
* Returns the offset of the first non-whitespace character in the specified document, searching
* right/downward from the specified start offset up to the specified end offset. If there is

View file

@ -1,12 +1,13 @@
/*******************************************************************************
* Copyright (c) 2008, 2009 Symbian Software Systems and others.
* Copyright (c) 2008, 2010 Symbian Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Andrew Ferguson (Symbian) - Initial implementation
* Andrew Ferguson (Symbian) - Initial implementation
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.ui.text.doctools.doxygen;
@ -17,6 +18,7 @@ import org.eclipse.jface.text.IAutoEditStrategy;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
@ -46,11 +48,13 @@ import org.eclipse.cdt.ui.text.doctools.DefaultMultilineCommentAutoEditStrategy;
public class DoxygenMultilineAutoEditStrategy extends DefaultMultilineCommentAutoEditStrategy {
private static final String SINGLELINE_COMMENT_PRECEDING = "//!< "; //$NON-NLS-1$
private static final String PARAM = "@param "; //$NON-NLS-1$
private static final String RETURN = "@return\n"; //$NON-NLS-1$
private static final String RETURN = "@return"; //$NON-NLS-1$
protected boolean documentPureVirtuals= true;
protected boolean documentDeclarations= true;
private String fLineDelimiter;
public DoxygenMultilineAutoEditStrategy() {
}
@ -88,12 +92,22 @@ public class DoxygenMultilineAutoEditStrategy extends DefaultMultilineCommentAut
StringBuilder result= new StringBuilder();
for(int i=0; i<decls.length; i++) {
if(!isVoidParameter(decls[i])) {
result.append(PARAM+getParameterName(decls[i])+"\n"); //$NON-NLS-1$
result.append(PARAM).append(getParameterName(decls[i])).append(getLineDelimiter());
}
}
return result;
}
/**
* Get the default line delimiter for the currently customized document
* which should be used for new lines.
*
* @return the default line delimiter
*/
private String getLineDelimiter() {
return fLineDelimiter;
}
/**
* @param decl
* @return the name of the parameter
@ -128,7 +142,7 @@ public class DoxygenMultilineAutoEditStrategy extends DefaultMultilineCommentAut
* @return the comment content to describe the return
*/
protected StringBuilder documentFunctionReturn() {
return new StringBuilder(RETURN);
return new StringBuilder(RETURN).append(getLineDelimiter());
}
/**
@ -154,6 +168,7 @@ public class DoxygenMultilineAutoEditStrategy extends DefaultMultilineCommentAut
*/
@Override
protected StringBuilder customizeAfterNewLineForDeclaration(IDocument doc, IASTDeclaration dec, ITypedRegion partition) {
fLineDelimiter = TextUtilities.getDefaultLineDelimiter(doc);
while(dec instanceof ICPPASTTemplateDeclaration) /* if? */
dec= ((ICPPASTTemplateDeclaration)dec).getDeclaration();