Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 317686

Summary: Implement method error
Product: [Tools] CDT Reporter: Álvaro <alvarofleith>
Component: cdt-refactoringAssignee: Marc-André Laperle <malaperle>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: jamesblackburn+eclipse, malaperle, yevshif
Version: 7.0   
Target Milestone: 8.0   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Workspace demonstrate bug
none
Handle linked resources
none
Handle linked resources
none
Handle linked resources, updated
none
Handle linked resources, updated
none
Improve search of implementation file
none
Handle linked resources, updated
none
Handle linked resources, updated malaperle: iplog-

Description Álvaro CLA 2010-06-23 08:09:34 EDT
Forward error example using "Implement Method" in "Source menu":

//Test.h file
class Test
{
public:
    int implementTest(int i,int j) const;
};

//Test.cpp file
inline int implementTest(int i, int j) const
{
}

Where is the scoped definition in implementation method ?
inline definition is not necessary!!

This correct is:
int Test::implementTest(int i, int j) const
{
}


-- Configuration Details --
Product: Eclipse 1.3.0.20100609-1425 (org.eclipse.epp.package.cpp.product)
Installed Features:
 org.eclipse.platform 3.6.0.v20100602-9gF78GpqFt6trOGhL5t0nJy5fyGHKrwNY
Comment 1 Marc-André Laperle CLA 2010-06-24 12:59:38 EDT
Hello Álvaro. I tried to reproduce the bug with the example you provided but it works for me. I've seen this bug many times (missing scope definition) but now that I want to fix it, I can't reproduce it. I will try to reproduce it in other scenarios but if you find a reliable way to reproduce it (reasonably sized), please let me know.
Comment 2 Álvaro CLA 2010-06-30 12:11:03 EDT
Created attachment 173120 [details]
Workspace demonstrate bug
Comment 3 Álvaro CLA 2010-06-30 12:17:08 EDT
(In reply to comment #1)
> Hello Álvaro. I tried to reproduce the bug with the example you provided but it
> works for me. I've seen this bug many times (missing scope definition) but now
> that I want to fix it, I can't reproduce it. I will try to reproduce it in
> other scenarios but if you find a reliable way to reproduce it (reasonably
> sized), please let me know.

I add attachment for you reproduce the bug. The bug only occurs in linked folders containing source code.

[]',
Álvaro.
Comment 4 Marc-André Laperle CLA 2010-07-01 01:01:02 EDT
Created attachment 173163 [details]
Handle linked resources

Thank you for the attachment, I can now reproduce it. This patch fixes the problem.
Comment 5 Marc-André Laperle CLA 2010-07-01 09:31:31 EDT
Can you change the Component of the bug to cdt-refactoring? Thanks.
Comment 6 Álvaro CLA 2010-07-01 11:34:00 EDT
(In reply to comment #5)
> Can you change the Component of the bug to cdt-refactoring? Thanks.
Yes.
You are done!
Comment 7 Marc-André Laperle CLA 2010-07-02 19:09:53 EDT
Created attachment 173336 [details]
Handle linked resources

Small tweak, findFilesForLocationURI handles both linked and non-linked resources.
Comment 8 Marc-André Laperle CLA 2011-01-12 12:48:51 EST
Created attachment 186656 [details]
Handle linked resources, updated

Updated the patch after changes in bug 334051.

Emanuel, does this look good to you?
Comment 9 James Blackburn CLA 2011-01-12 12:52:32 EST
You should consider using CDT's ResourceLookup#selectFileForLocation or findFilesForLocation instead of calling straight through to the platform.
Comment 10 Marc-André Laperle CLA 2011-01-12 23:29:54 EST
Created attachment 186702 [details]
Handle linked resources, updated

Hi James, thanks for the suggestion. Is this patch better? I assume using ResourceLookup is better for performance?
Comment 11 James Blackburn CLA 2011-01-13 07:04:20 EST
I was thinking something more like:

IPath path = file.getLocation().removeFileExtension().addFileExtension("cpp");  //$NON-NLS-1$
IFile fileForLocation = ResourceLookup.selectFileForLocation(path, file.getProject());

ResourceLookup is designed for siutations such as these, it selects the most appropriate IFile based on CDT criteria (existing in the project, living under a source folder, etc.).  It's nearly always wrong to do #findFilesForLocation()[0] as why should file 0 be preferred to file[1] or 2 or whatever...?

Apart from that you patch looks fine to me. 

There are a couple things I don't understand:
  - Why is ".cpp" a hard-coded extension?  Surely any cpp content type should be accepted?
  - Why does the code only handle files in the same directory?

I would have thought that any c++ file in the project (with the same name) should be considered?
Comment 12 Marc-André Laperle CLA 2011-02-02 12:08:48 EST
Created attachment 188169 [details]
Improve search of implementation file

(In reply to comment #11)
> There are a couple things I don't understand:
>   - Why is ".cpp" a hard-coded extension?  Surely any cpp content type should
> be accepted?
>   - Why does the code only handle files in the same directory?
> 
> I would have thought that any c++ file in the project (with the same name)
> should be considered?

I have changed the patch to improve this. I extracted some code from ToggleSourceAndHeaderAction and moved it to a new Util class, ToggleSourceAndHeaderUtil. Both 'Implement method' and 'Generate Getters and Setters' now use this code and I added some tests. In the case of 'Generate Getters and Setters', it was always failing to find an implementation file if there was no definition in it yet. This is due to the new MethodDefinitionInsertLocationFinder2 which doesn't use the .cpp 'fallback'. The patch looks big but it's mostly moving stuff to a new Util class.
Comment 13 Marc-André Laperle CLA 2011-02-12 11:58:40 EST
Created attachment 188836 [details]
Handle linked resources, updated

I'll create a new bug for the ToggleSourceAndHeaderUtil stuff, I think it's a bit out of the scope of this bug. This new patch uses selectFileForLocation as suggested. James, does this look good to you?
Comment 14 Marc-André Laperle CLA 2011-02-20 17:28:43 EST
Created attachment 189387 [details]
Handle linked resources, updated

Updated the patch after bug 337040 was resolved. Now it's a one line change.
Comment 15 Marc-André Laperle CLA 2011-03-07 21:54:38 EST
Comment on attachment 189387 [details]
Handle linked resources, updated

Fixed in HEAD.
Comment 16 Marc-André Laperle CLA 2011-03-07 21:56:16 EST
Fixed. (Didn't expect I'd have to comment)
Comment 17 CDT Genie CLA 2011-03-07 22:23:26 EST
*** cdt cvs genie on behalf of mlaperle ***
Bug 317686 - Implement method error. Fixed issue with linked resources.

[*] NamespaceHelper.java 1.7 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt/all/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/utils/NamespaceHelper.java?root=Tools_Project&r1=1.6&r2=1.7