Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtaneja/text blocks 1 #30

Open
wants to merge 5 commits into
base: jdk/amber/string-tapas
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,11 @@ public void testIncDecReading230408() throws Exception {

public void testRawStringLiteral() throws Exception {
try {
SourceVersion.valueOf("RELEASE_11");
SourceVersion.valueOf("RELEASE_13");
} catch (IllegalArgumentException iae) {
//OK, presumably no support for raw string literals
}
setSourceLevel("11");
setSourceLevel("13");
performTest("RawStringLiteral",
"public class RawStringLiteral {\n" +
" String s1 = \"\"\"\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import java.util.List;
import java.util.Set;
import java.util.prefs.Preferences;
import javax.lang.model.SourceVersion;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.netbeans.api.editor.mimelookup.MimeLookup;
import org.netbeans.api.editor.settings.SimpleValueNames;
import org.netbeans.api.java.lexer.JavaTokenId;
import static org.netbeans.api.java.source.SourceUtils.isTextBlockSupported;
import org.netbeans.api.lexer.PartType;
import org.netbeans.api.lexer.TokenHierarchy;
import org.netbeans.api.lexer.TokenSequence;
Expand Down Expand Up @@ -144,7 +146,7 @@ static void completeOpeningBracket(TypedTextInterceptor.MutableContext context)
char insChr = context.getText().charAt(0);
context.setText("" + insChr + matching(insChr), 1); // NOI18N
} else if (chr == '\"') {
if (context.getOffset() > 2 && context.getDocument().getText(context.getOffset() - 2, 3).equals("\"\"\"")) {
if (isTextBlockSupported() && (context.getOffset() > 2 && context.getDocument().getText(context.getOffset() - 2, 3).equals("\"\"\""))) {
context.setText("\"\n\"\"\"", 2); // NOI18N
} else {
context.setText("\"\"", 1); // NOI18N
Expand Down Expand Up @@ -210,7 +212,6 @@ static int completeQuote(TypedTextInterceptor.MutableContext context) throws Bad
TokenSequence<JavaTokenId> javaTS = javaTokenSequence(context, true);
JavaTokenId id = (javaTS != null) ? javaTS.token().id() : null;


// If caret within comment return false
boolean caretInsideToken = (id != null)
&& (javaTS.offset() + javaTS.token().length() > context.getOffset()
Expand Down Expand Up @@ -251,12 +252,22 @@ static int completeQuote(TypedTextInterceptor.MutableContext context) throws Bad
}

if ((completablePosition && !insideString) || eol) {
if (context.getText().equals("\"") && context.getOffset() >= 2 && context.getDocument().getText(context.getOffset() - 2, 2).equals("\"\"")) {
if (isTextBlockSupported() && context.getText().equals("\"") && context.getOffset() >= 2 && context.getDocument().getText(context.getOffset() - 2, 2).equals("\"\"")) {
context.setText("\"\n\"\"\"", 2); // NOI18N
Thread.dumpStack();
} else {
context.setText(context.getText() + context.getText(), 1);
}
} else if (isTextBlockSupported() && context.getText().equals("\"")) {
if ((javaTS != null) && javaTS.moveNext()) {
id = javaTS.token().id();
if ((id == JavaTokenId.STRING_LITERAL) && (javaTS.token().text().toString().equals("\"\""))) {
if (context.getDocument().getText(context.getOffset(), 2).equals("\"\"")) {
context.setText("\"\"\"\n\"", 4);
}
}
javaTS.movePrevious();
id = javaTS.token().id();
}
}
return -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.api.java.source.ModificationResult;
import org.netbeans.api.java.source.SourceUtils;
import static org.netbeans.api.java.source.SourceUtils.isTextBlockSupported;
import org.netbeans.api.java.source.Task;
import org.netbeans.api.java.source.WorkingCopy;
import org.netbeans.api.lexer.TokenHierarchy;
Expand All @@ -98,6 +99,7 @@
import org.netbeans.editor.BaseKit.CutAction;
import org.netbeans.lib.editor.util.swing.DocumentUtilities;
import org.netbeans.modules.editor.NbEditorUtilities;
import org.netbeans.modules.editor.indent.api.IndentUtils;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.filesystems.FileObject;
Expand Down Expand Up @@ -621,46 +623,102 @@ public boolean importData(TransferSupport support) {

private boolean delegatedImportData(final TransferSupport support) {
JComponent comp = (JComponent) support.getComponent();
if (comp instanceof JTextComponent && !support.isDataFlavorSupported(COPY_FROM_STRING_FLAVOR) && insideToken((JTextComponent) comp, JavaTokenId.STRING_LITERAL)) {
final Transferable t = support.getTransferable();
return delegate.importData(comp, new Transferable() {
@Override
public DataFlavor[] getTransferDataFlavors() {
return t.getTransferDataFlavors();
}
if (comp instanceof JTextComponent && !support.isDataFlavorSupported(COPY_FROM_STRING_FLAVOR) ) {
if (insideToken((JTextComponent) comp, JavaTokenId.STRING_LITERAL)) {
final Transferable t = support.getTransferable();
return delegate.importData(comp, new Transferable() {
@Override
public DataFlavor[] getTransferDataFlavors() {
return t.getTransferDataFlavors();
}

@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return t.isDataFlavorSupported(flavor);
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return t.isDataFlavorSupported(flavor);
}

@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
Object data = t.getTransferData(flavor);
if (data instanceof String) {
String s = (String) data;
s = s.replace("\\","\\\\"); //NOI18N
s = s.replace("\"","\\\""); //NOI18N
s = s.replace("\r\n","\n"); //NOI18N
s = s.replace("\n","\\n\" +\n\""); //NOI18N
data = s;
} else if (data instanceof Reader) {
BufferedReader br = new BufferedReader((Reader)data);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
line = line.replace("\\","\\\\"); //NOI18N
line = line.replace("\"","\\\""); //NOI18N
if (sb.length() > 0) {
sb.append("\\n\" +\n\""); //NOI18N
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
Object data = t.getTransferData(flavor);
if (data instanceof String) {
String s = (String) data;
s = s.replace("\\","\\\\"); //NOI18N
s = s.replace("\"","\\\""); //NOI18N
s = s.replace("\r\n","\n"); //NOI18N
s = s.replace("\n","\\n\" +\n\""); //NOI18N
data = s;
} else if (data instanceof Reader) {
BufferedReader br = new BufferedReader((Reader)data);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
line = line.replace("\\","\\\\"); //NOI18N
line = line.replace("\"","\\\""); //NOI18N
if (sb.length() > 0) {
sb.append("\\n\" +\n\""); //NOI18N
}
sb.append(line);
}
sb.append(line);
data = new StringReader(sb.toString());
}
data = new StringReader(sb.toString());
return data;
}
return data;
}
});
});
} else if ((isTextBlockSupported()) && insideToken((JTextComponent) comp, JavaTokenId.MULTILINE_STRING_LITERAL)) {
final Transferable t = support.getTransferable();
return delegate.importData(comp, new Transferable() {
@Override
public DataFlavor[] getTransferDataFlavors() {
return t.getTransferDataFlavors();
}

@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return t.isDataFlavorSupported(flavor);
}

@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
Object data = t.getTransferData(flavor);
JTextComponent c = (JTextComponent) comp;
int indent = 0;
try {
indent = IndentUtils.lineIndent(c.getDocument(), IndentUtils.lineStartOffset(c.getDocument(), c.getCaretPosition()));
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
if (data instanceof String) {
String s = (String) data;
s = s.replace("\"\"\"","\\\"\"\""); //NOI18N
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < indent; i++) {
sb.append(" "); //NOI18N
}
String emptySpaces = sb.toString();
s = s.replace("\r\n","\n"); //NOI18N
s = s.replace("\n",System.lineSeparator() + emptySpaces); //NOI18N
data = s;
} else if (data instanceof Reader) {
BufferedReader br = new BufferedReader((Reader)data);
StringBuilder sb = new StringBuilder();
String line;

while ((line = br.readLine()) != null) {
line = line.replace("\"\"\"", "\\\"\"\""); //NOI18N
if (sb.length() > 0) {
sb.append(System.lineSeparator()); //NOI18N
for (int i = 0; i < indent; i++) {
sb.append(" "); //NOI18N
}
}
sb.append(line);
}
data = new StringReader(sb.toString());
}
return data;
}
});
}
}
return delegate.importData(support);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.awt.event.KeyEvent;
import java.util.prefs.Preferences;
import java.util.regex.Pattern;
import javax.lang.model.SourceVersion;
import javax.swing.JEditorPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
Expand Down Expand Up @@ -1241,29 +1242,65 @@ public void testSkipBracketInComment() throws Exception {
}

public void testTextBlock1() throws Exception {
try {
SourceVersion.valueOf("RELEASE_13");
} catch (IllegalArgumentException ex) {
//OK, skip test
return ;
}
Context ctx = new Context(new JavaKit(), "\"\"|");
ctx.typeChar('\"');
ctx.assertDocumentTextEquals("\"\"\"\n|\"\"\"");
}

public void testTextBlock2() throws Exception {
try {
SourceVersion.valueOf("RELEASE_13");
} catch (IllegalArgumentException ex) {
//OK, skip test
return ;
}
Context ctx = new Context(new JavaKit(), "\"\"\"\n|\"\"\"");
ctx.typeChar('\"');
ctx.assertDocumentTextEquals("\"\"\"\n\"|\"\"");
}

public void testTextBlock3() throws Exception {
try {
SourceVersion.valueOf("RELEASE_13");
} catch (IllegalArgumentException ex) {
//OK, skip test
return ;
}
Context ctx = new Context(new JavaKit(), "\"\"\"\n\"|\"\"");
ctx.typeChar('\"');
ctx.assertDocumentTextEquals("\"\"\"\n\"\"|\"");
}

public void testTextBlock4() throws Exception {
try {
SourceVersion.valueOf("RELEASE_13");
} catch (IllegalArgumentException ex) {
//OK, skip test
return ;
}
Context ctx = new Context(new JavaKit(), "\"\"\"\n\"\"|\"");
ctx.typeChar('\"');
ctx.assertDocumentTextEquals("\"\"\"\n\"\"\"|");
}


public void testTextBlock5() throws Exception {
try {
SourceVersion.valueOf("RELEASE_13");
} catch (IllegalArgumentException ex) {
//OK, skip test:
return ;
}
Context ctx = new Context(new JavaKit(), "t(|\"\")");
ctx.typeChar('\"');
ctx.assertDocumentTextEquals("t(\"\"\"\n |\"\"\")");
}

public void testCorrectHandlingOfStringEscapes184059() throws Exception {
assertTrue(isInsideString("foo\n\"bar|\""));
assertTrue(isInsideString("foo\n\"bar\\\"|\""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import javax.lang.model.SourceVersion;
import javax.swing.JEditorPane;
import javax.swing.SwingUtilities;
import org.netbeans.api.java.lexer.JavaTokenId;
Expand Down Expand Up @@ -90,6 +91,20 @@ public void testAnnotationWithValueCopied() throws Exception {
public void testAnonymousClass() throws Exception {
copyAndPaste("package test;\nimport java.util.ArrayList;\npublic class Test { void t() { |new ArrayList<String>() {};| } }\n", "package test;\npublic class Target {\nvoid t() { ^ }\n}", "package test;\n\nimport java.util.ArrayList;\n\npublic class Target {\nvoid t() { new ArrayList<String>() {}; }\n}");
}

public void testCopyIntoTextBlock() throws Exception {
copyAndPaste("|List l1;\nList l2;\nList l3;\n\n| ", "package test;\npublic class Target {\nString s = \"\"\"\n^\"\"\"\n}", "package test;\npublic class Target {\nString s = \"\"\"\nList l1;\nList l2;\nList l3;\n\n\"\"\"\n}");
}

public void testCopyTextBlockIntoTextBlock() throws Exception {
try {
SourceVersion.valueOf("RELEASE_13");
} catch (IllegalArgumentException ex) {
//OK, skip test
return ;
}
copyAndPaste("|\"\"\"\nList l1;\"\"\"| ", "package test;\npublic class Target {\nString s = \"\"\"\ntest^ block\n\"\"\"\n}", "package test;\npublic class Target {\nString s = \"\"\"\ntest\\\"\"\"\nList l1;\\\"\"\" block\n\"\"\"\n}");
}

private void copyAndPaste(String from, final String to, String golden) throws Exception {
final int pastePos = to.indexOf('^');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sun.source.tree.Tree.Kind;
import com.sun.source.util.TreePath;
import java.util.List;
import org.netbeans.api.java.queries.CompilerOptionsQuery;
import org.netbeans.api.java.source.CompilationInfo;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.Fix;
Expand All @@ -36,7 +37,8 @@
import org.netbeans.spi.java.hints.TriggerTreeKind;
import org.openide.util.NbBundle.Messages;

@Hint(displayName = "#DN_ConvertToTextBlock", description = "#DESC_ConvertToTextBlock", category = "general")
@Hint(displayName = "#DN_ConvertToTextBlock", description = "#DESC_ConvertToTextBlock", category="rules15",
minSourceVersion = "13")
@Messages({
"DN_ConvertToTextBlock=Convert to Text Block",
"DESC_ConvertToTextBlock=Convert to Text Block"
Expand All @@ -46,6 +48,8 @@ public class ConvertToTextBlock {
@TriggerTreeKind(Kind.PLUS)
@Messages("ERR_ConvertToTextBlock=Can be converted to text block")
public static ErrorDescription computeWarning(HintContext ctx) {
if (!CompilerOptionsQuery.getOptions(ctx.getInfo().getFileObject()).getArguments().contains("--enable-preview"))
return null;
if (ctx.getPath().getParentPath() != null && getTextOrNull(ctx.getPath().getParentPath()) != null) {
return null;
}
Expand Down
Loading