1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-07 10:33:26 +02:00

Bug 270916 - Automatically close of quotes in preprocessor directives does not work

This commit is contained in:
Anton Leherbauer 2009-11-19 08:38:47 +00:00
parent 2318b0ea37
commit 95c5fce830
2 changed files with 37 additions and 3 deletions

View file

@ -303,11 +303,26 @@ public class BracketInserterTest extends TestCase {
setCaret(BODY_OFFSET);
type("#define MACRO ");
int offset = getCaret();
// enter opening quote (should be closed again)
type('"');
assertEquals("\"\"", fDocument.get(offset, 2));
assertSingleLinkedPosition(offset + 1);
// enter closing quote (should not add a quote, but proceed cursor)
type('"');
assertEquals("\"\"", fDocument.get(offset, 2));
assertEquals(offset + 2, getCaret());
// delete closing quote and enter quote again
type(SWT.BS);
assertEquals("\"", fDocument.get(offset, 1));
int length = fDocument.getLength();
type('"');
assertEquals("\"\"", fDocument.get(offset, 2));
assertEquals(offset + 2, getCaret());
assertEquals(length + 1, fDocument.getLength());
}
public void testPreferences() throws BadLocationException, CModelException, CoreException {

View file

@ -53,6 +53,7 @@ import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.DefaultLineTracker;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension;
@ -643,7 +644,8 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
case '"':
if (!fCloseStrings
|| nextToken == Symbols.TokenIDENT
|| next != null && (next.length() > 1 || next.charAt(0) == '"'))
|| next != null && (next.length() > 1 || next.charAt(0) == event.character)
|| isInsideStringInPreprocessorDirective(partition, document, offset))
return;
break;
@ -661,7 +663,7 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
buffer.append(closingCharacter);
if (closingCharacter == '>' && nextToken != Symbols.TokenEOF
&& document.getChar(offset + length) == '>') {
// Insert a space to avoid two consequtive closing angular brackets.
// Insert a space to avoid two consecutive closing angular brackets.
buffer.append(' ');
}
@ -710,6 +712,23 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IC
}
}
private boolean isInsideStringInPreprocessorDirective(ITypedRegion partition, IDocument document, int offset) throws BadLocationException {
if (ICPartitions.C_PREPROCESSOR.equals(partition.getType())) {
// use temporary document to test whether offset is inside non-default partition
String directive = document.get(partition.getOffset(), offset - partition.getOffset() + 1);
int hashIdx = directive.indexOf('#');
if (hashIdx >= 0) {
IDocument tmp = new Document(directive.substring(hashIdx + 1));
new CDocumentSetupParticipant().setup(tmp);
String type = TextUtilities.getContentType(tmp, ICPartitions.C_PARTITIONING, offset - (partition.getOffset() + hashIdx + 1), true);
if (!type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
return true;
}
}
}
return false;
}
/*
* @see org.eclipse.jface.text.link.ILinkedModeListener#left(org.eclipse.jface.text.link.LinkedModeModel, int)
*/