mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-15 04:05:38 +02:00
Bug 510794 - Tab character is ignored at the beginning of line inside a
raw string literal Change-Id: Idc8dfba8900495933f681f809dc96c4d92a6db8a
This commit is contained in:
parent
fa86617aec
commit
1d6cf48198
4 changed files with 57 additions and 25 deletions
|
@ -0,0 +1,3 @@
|
||||||
|
const char* s = R"(
|
||||||
|
Some text
|
||||||
|
)";
|
|
@ -0,0 +1,3 @@
|
||||||
|
const char* s = R"(
|
||||||
|
Some text
|
||||||
|
)";
|
|
@ -8,16 +8,12 @@
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* IBM Corporation - initial API and implementation
|
* IBM Corporation - initial API and implementation
|
||||||
* Anton Leherbauer (Wind River Systems)
|
* Anton Leherbauer (Wind River Systems)
|
||||||
|
* Sergey Prigogin (Google)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.ui.tests.text;
|
package org.eclipse.cdt.ui.tests.text;
|
||||||
|
|
||||||
import java.util.ListResourceBundle;
|
import java.util.ListResourceBundle;
|
||||||
|
|
||||||
import junit.extensions.TestSetup;
|
|
||||||
import junit.framework.Test;
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
import junit.framework.TestSuite;
|
|
||||||
|
|
||||||
import org.eclipse.jface.text.IDocument;
|
import org.eclipse.jface.text.IDocument;
|
||||||
import org.eclipse.jface.text.source.SourceViewer;
|
import org.eclipse.jface.text.source.SourceViewer;
|
||||||
|
|
||||||
|
@ -31,6 +27,11 @@ import org.eclipse.cdt.ui.testplugin.ResourceTestHelper;
|
||||||
import org.eclipse.cdt.internal.ui.actions.IndentAction;
|
import org.eclipse.cdt.internal.ui.actions.IndentAction;
|
||||||
import org.eclipse.cdt.internal.ui.editor.CEditor;
|
import org.eclipse.cdt.internal.ui.editor.CEditor;
|
||||||
|
|
||||||
|
import junit.extensions.TestSetup;
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the IndentAction.
|
* Test the IndentAction.
|
||||||
*/
|
*/
|
||||||
|
@ -46,15 +47,15 @@ public class IndentActionTest extends TestCase {
|
||||||
|
|
||||||
protected static class IndentTestSetup extends TestSetup {
|
protected static class IndentTestSetup extends TestSetup {
|
||||||
private ICProject fCProject;
|
private ICProject fCProject;
|
||||||
|
|
||||||
public IndentTestSetup(Test test) {
|
public IndentTestSetup(Test test) {
|
||||||
super(test);
|
super(test);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
|
||||||
fCProject= EditorTestHelper.createCProject(PROJECT, "resources/indentation");
|
fCProject= EditorTestHelper.createCProject(PROJECT, "resources/indentation");
|
||||||
fCProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.TAB);
|
fCProject.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.TAB);
|
||||||
}
|
}
|
||||||
|
@ -63,11 +64,11 @@ public class IndentActionTest extends TestCase {
|
||||||
protected void tearDown () throws Exception {
|
protected void tearDown () throws Exception {
|
||||||
if (fCProject != null)
|
if (fCProject != null)
|
||||||
CProjectHelper.delete(fCProject);
|
CProjectHelper.delete(fCProject);
|
||||||
|
|
||||||
super.tearDown();
|
super.tearDown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Test suite() {
|
public static Test suite() {
|
||||||
return new IndentTestSetup(new TestSuite(IndentActionTest.class));
|
return new IndentTestSetup(new TestSuite(IndentActionTest.class));
|
||||||
}
|
}
|
||||||
|
@ -83,18 +84,18 @@ public class IndentActionTest extends TestCase {
|
||||||
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
|
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
|
||||||
fDocument= fSourceViewer.getDocument();
|
fDocument= fSourceViewer.getDocument();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void tearDown() throws Exception {
|
protected void tearDown() throws Exception {
|
||||||
EditorTestHelper.closeEditor(fEditor);
|
EditorTestHelper.closeEditor(fEditor);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertIndentResult() throws Exception {
|
private void assertIndentResult(boolean isTabAction) throws Exception {
|
||||||
String afterFile= createFileName("After");
|
String afterFile= createFileName("After");
|
||||||
String expected= ResourceTestHelper.read(afterFile).toString();
|
String expected= ResourceTestHelper.read(afterFile).toString();
|
||||||
|
|
||||||
new IndentAction(new EmptyBundle(), "prefix", fEditor, false).run();
|
new IndentAction(new EmptyBundle(), "prefix", fEditor, isTabAction).run();
|
||||||
|
|
||||||
assertEquals(expected, fDocument.get());
|
assertEquals(expected, fDocument.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,23 +104,29 @@ public class IndentActionTest extends TestCase {
|
||||||
name= name.substring(4, 5).toLowerCase() + name.substring(5);
|
name= name.substring(4, 5).toLowerCase() + name.substring(5);
|
||||||
return "/" + PROJECT + "/src/" + name + "/" + qualifier + ".cpp";
|
return "/" + PROJECT + "/src/" + name + "/" + qualifier + ".cpp";
|
||||||
}
|
}
|
||||||
|
|
||||||
private void selectAll() {
|
private void selectAll() {
|
||||||
fSourceViewer.setSelectedRange(0, fDocument.getLength());
|
fSourceViewer.setSelectedRange(0, fDocument.getLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testUnchanged() throws Exception {
|
public void testUnchanged() throws Exception {
|
||||||
selectAll();
|
selectAll();
|
||||||
assertIndentResult();
|
assertIndentResult(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSample() throws Exception {
|
public void testSample() throws Exception {
|
||||||
selectAll();
|
selectAll();
|
||||||
assertIndentResult();
|
assertIndentResult(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testComplex() throws Exception {
|
public void testComplex() throws Exception {
|
||||||
selectAll();
|
selectAll();
|
||||||
assertIndentResult();
|
assertIndentResult(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// See http://bugs.eclipse.org/510794
|
||||||
|
public void testRawString() throws Exception {
|
||||||
|
fSourceViewer.setSelectedRange(fDocument.getLineOffset(1) + 1, 0);
|
||||||
|
assertIndentResult(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* IBM Corporation - initial API and implementation
|
* IBM Corporation - initial API and implementation
|
||||||
* Sergey Prigogin, Google
|
* Sergey Prigogin (Google)
|
||||||
* Anton Leherbauer (Wind River Systems)
|
* Anton Leherbauer (Wind River Systems)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.ui.actions;
|
package org.eclipse.cdt.internal.ui.actions;
|
||||||
|
@ -212,8 +212,27 @@ public class IndentAction extends TextEditorAction {
|
||||||
} else if (startingPartition.getType().equals(ICPartitions.C_PREPROCESSOR)) {
|
} else if (startingPartition.getType().equals(ICPartitions.C_PREPROCESSOR)) {
|
||||||
indent= computePreprocessorIndent(document, line, startingPartition);
|
indent= computePreprocessorIndent(document, line, startingPartition);
|
||||||
} else if (startingPartition.getType().equals(ICPartitions.C_STRING) && offset > startingPartition.getOffset()) {
|
} else if (startingPartition.getType().equals(ICPartitions.C_STRING) && offset > startingPartition.getOffset()) {
|
||||||
// don't indent inside (raw-)string
|
// Don't indent inside (raw-)string, but if the indent action was triggered by a Tab key,
|
||||||
return false;
|
// insert a '\t' character or spaces at the caret position.
|
||||||
|
if (!fIsTabAction)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
String text = "\t"; //$NON-NLS-1$
|
||||||
|
if (useSpaces()) {
|
||||||
|
int tabSize = getTabSize();
|
||||||
|
if (tabSize == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int numSpaces = tabSize - (caret - offset) % tabSize;
|
||||||
|
StringBuilder buf = new StringBuilder(numSpaces);
|
||||||
|
for (int i = 0; i < numSpaces; i++) {
|
||||||
|
buf.append(' ');
|
||||||
|
}
|
||||||
|
text = buf.toString();
|
||||||
|
}
|
||||||
|
document.replace(caret, 0, text);
|
||||||
|
fCaretOffset= caret + text.length();
|
||||||
|
return true;
|
||||||
} else if (!fIsTabAction && startingPartition.getOffset() == offset && startingPartition.getType().equals(ICPartitions.C_SINGLE_LINE_COMMENT)) {
|
} else if (!fIsTabAction && startingPartition.getOffset() == offset && startingPartition.getType().equals(ICPartitions.C_SINGLE_LINE_COMMENT)) {
|
||||||
// line comment starting at position 0 -> indent inside
|
// line comment starting at position 0 -> indent inside
|
||||||
if (indentInsideLineComments()) {
|
if (indentInsideLineComments()) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue