简体   繁体   中英

Extract or copy a string value from declaration code in Eclipse (IDE feature)

Assume the following String declarations:

final String WAY_TO_JINGLE = "all the way";
String vehicleName = Transport.getVehicleName("Santa Claus");

String example =
    "Jingle bells, jingle bells, " +
    "jingle " + WAY_TO_JINGLE + '.' + System.lineSeparator() +
    "Oh! what fun it is to ride " +
    "\tIn a one-horse open " + vehicleName + '.';

Is there a feature in or plugin for Eclipse which would allow me to copy to clipboard, the declared string from this code?

For example, using it on the declaration of example would put the following text in clipboard (assuming I entered "sleigh" as the value for vehicleName ):

Jingle bells, jingle bells, jingle all the way.
Oh! what fun it is to ride     In a one-horse open sleigh.

It should do the following:

  • Get known constant or variable values from their declarations.
  • Show a dialog form to fill up values for unknown variables, possibly pre-filling best guess or variable name itself .
  • Parse the escape characters in the string literal.
  • Be aware of System tools like lineSeparator() method and "line.separator" property.

Currently there is no such a feature or plugin (or it is undiscovered by me). The closest I can get is by using the Scrapbook feature in Eclipse (many thanks to Elliott Frisch ).

  1. In Eclipse, click File > New > Other or press Ctrl N .
  2. Type in scrapbook and select Java > Java Run/Debug > Scrapbook Page .
  3. Click Next , select a folder, enter a file name, and click Finish .

  4. Once in the scrapbook, paste the code snippet. Replace all method references with actual strings:

     final String WAY_TO_JINGLE = "all the way"; String vehicleName = "sleigh"; String example = "Jingle bells, jingle bells, " + "jingle " + WAY_TO_JINGLE + '.' + '\\n' + "Oh! what fun it is to ride " + "\\tIn a one-horse open " + vehicleName + '.'; 
  5. Then add a return statement:

     return example; 
  6. Select all of it, and click the Display button or press Ctrl Shift D .

If there are errors, the error descriptions will be added before the selected text. Correct the errors, delete the descriptions, and try the last step again.

The method lineSeparator() is undefined for the type System
final String WAY_TO_JINGLE = "all the way";

If there are no errors, the returned value will be added after the selected text along with the return type.

return example;
(java.lang.String) Jingle bells, jingle bells, jingle all the way.
Oh! what fun it is to ride  In a one-horse open sleigh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM