This Bugzilla instance is deprecated, and most Eclipse projects now use GitHub or Eclipse GitLab. Please see the deprecation plan for details.
Bug 474326 - Copying a snippet does not copy the code
Summary: Copying a snippet does not copy the code
Status: CLOSED FIXED
Alias: None
Product: WTP Common Tools
Classification: WebTools
Component: Snippets Framework (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: Future   Edit
Assignee: Roberto Sanchez Herrera CLA
QA Contact: Nitin Dahyabhai CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-08-05 09:43 EDT by Alex Moore CLA
Modified: 2017-12-07 14:02 EST (History)
6 users (show)

See Also:
shr31223: review? (thatnitind)


Attachments
Steps to recreate the problem (1.07 MB, application/msword)
2015-08-05 09:43 EDT, Alex Moore CLA
no flags Details
Proposed patch v1 (3.44 KB, patch)
2016-01-20 16:45 EST, Roberto Sanchez Herrera CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Moore CLA 2015-08-05 09:43:35 EDT
Created attachment 255645 [details]
Steps to recreate the problem

Customer created a new category in Customize Palette and inserted code into the template.  After completing the customize palette he right click on the item & copy.  Then right click in a empty place and paste.  The paste completes successfully but the data in the template does not copy over.

Code in the Template:
          // ${Field_desc}  (${Field_name})
          If OldRec.$(Field_name) <> NewRec.${Field_name};
             WriteLog(Incom : InVendo : '*CHG' : '${Field_name}';
                     OldRec.${Field_name} : NewRec.${Field_name});
          Endif;
Comment 1 Steve Francisco CLA 2015-08-05 10:17:11 EDT
Moving to RSE
Comment 2 Steve Francisco CLA 2015-08-05 10:39:06 EDT
moving again to wst since the plugin is org.eclipse.wst.common.snippets
Comment 3 Alex Moore CLA 2015-08-11 10:45:13 EDT
Following up checking to see if anyone has had a chance to take a look at this issue yet?
Comment 4 Roberto Sanchez Herrera CLA 2015-08-27 12:44:58 EDT
Hello Alex, 
I tried to recreate the problem in WTP 3.6 and 3.7, and I couldn't. But I notice that in version, you put 3.4. So I also tested in 3.4, and I could not reproduce. 

Do you see an exception in the log? Can you reproduce it?
Comment 5 Alex Moore CLA 2015-10-21 11:31:47 EDT
Hi Roberto,

Below is the update from RDi development:
"I can consistently reproduce the problem with base eclipse 4.4

please reopen the bug on eclipse with the following steps to reproduce

open eclipse
create new category in snippets view
create new item under category with multiline template
select copy on item
select paste on category
select customize on new copy -> has complete multiline template as expected
open notepad, enter multiline text, select text, select copy
in snippets view, select copy on 1st item again
select paste on category
select customize on new copy -> only has 1st line of template"
Comment 6 Alex Moore CLA 2015-11-20 18:13:08 EST
Hi Roberto,

Checking to see if you had a chance to revisit this problem.
Comment 7 Alex Moore CLA 2015-12-18 19:06:30 EST
Hi Roberto,

I'm checking on the status of this problem.

Thanks...Alex
Comment 8 Roberto Sanchez Herrera CLA 2016-01-05 13:08:37 EST
Alex, 
I'm finally able to recreate the issue. The key point to recreate it is to create a new item with multiple lines, save it, restart eclipse, and then do the copy/paste. If you first use the Customize option and change something in the resulting dialog and press OK, then I cannot recreate the issue. 

The problem is that when the snippets are first loaded, the EOL characters are replaced to use the OS characters (in Windows, \r\n). This is done in ModelFactoryForUsers.createContent(Node item). After doing this, the content of the snippet is serialized to XML like this:

<?xml version="1.0" encoding="UTF-8"?><snippets>
    <item category="category_1451959391137" class="" editorclass="" id="category_14519593911371452011612295" label="Logging" smallicon="" snippetProvider="org.eclipse.wst.common.snippets.ui.TextSnippetProvider">
        <description><![CDATA[Logging field changes]]></description>
        <content><![CDATA[          // ${Field_desc}  (${Field_name})]]>&#13;<![CDATA[
          If OldRec.$(Field_name) <> NewRec.${Field_name};]]>&#13;<![CDATA[
             WriteLog(Incom : InVendo : '*CHG' : '${Field_name}';]]>&#13;<![CDATA[
                     OldRec.${Field_name} : NewRec.${Field_name});]]>&#13;<![CDATA[
          Endif;]]></content>
    </item>
</snippets>


Note that there are several CDATA sections in the content section. When the new snippet is created for the Paste action, only the first CDATA section is used (the first line)

If you use the Customize dialog, and do something (like deleting a category or snippet), the EOL character of the snippet is changed to \n (in variableItemEditor.updateItem()), which makes the snippet to be serialized with only one CDATA section with all the contents, and that is when the problem does not occur. 

So, I know the cause of the problem now, but at this moment I do not know the best way to fix it. I see a few methods messing around with line delimiters in this snippets code :\
Comment 9 Roberto Sanchez Herrera CLA 2016-01-20 16:45:58 EST
Created attachment 259287 [details]
Proposed patch v1
Comment 10 Roberto Sanchez Herrera CLA 2016-01-20 16:49:58 EST
Nitin, could you take a look at the attached patch?

I tried to make this patch the least invasive possible, so I decided to add code to the Copy action to replace line delimiters before serializing to XML, to prevent the problem. This also works for the Cut action (Cut/paste also removes the lines of the snippet). 
I tested this in Windows, I have not tested it in Linux
Comment 11 Edmund Reinhardt CLA 2017-10-31 13:37:02 EDT
Nitin
We are wondering if this has been addressed, since we are not seeing this in 4.6.3