User forums > Using Code::Blocks
Bug in 'swap header / source'
ironhead:
--- Quote from: Zini on October 20, 2009, 03:58:48 pm ---Still haven't got around to test your patch (sorry, so busy atm. its insane!), but IIRC it stops the feature from working when h and cpp files sit in different directories. That would break the feature completely for me (and most likely a lot of other people).
--- End quote ---
I think it all depends on your file structure, since the existing implementation also breaks if you have multiple subdirectories with source and include directories in the subdirectories.
--- Quote ---I agree that a solutation that handles all cases isn't possible without storing information about the source layout in the project file. However the situation you mentioned is handled perfectly by my algorithm. Did you have to time to take a look at it?
--- End quote ---
I'm in much the same position as you at the moment unfortunately (having very little time to work on things), hopefully sometime early next week I can take a look at implementing it. If you have some time before that and feel inclined at taking a stab at it, I'd be happy to help out. :D
Zini:
--- Quote ---I think it all depends on your file structure, since the existing implementation also breaks if you have multiple subdirectories with source and include directories in the subdirectories.
--- End quote ---
Not really. I am currently on SVN rev 5679. The swap feature works here most of the time (without automatically creating missing files though), unless there is more than one file with the same name (which has never worked properly with any version of Code::Blocks). That is still better than to break the feature entirely for certain layouts.
Zini:
--- Quote ---If you have some time before that and feel inclined at taking a stab at it, I'd be happy to help out.
--- End quote ---
Rather unlikely. I just had another look at the code. The time I would need to familiarize myself with WxWidgets API (never worked with it before) and the relevant Code::Blocks API (know nothing about it either) would most likely exceed any amount of time I can make available.
However I might be able to refine the algorithm a bit, if you are willing to try implementing it. I have a few more ideas, that should cover some additional edge-cases.
Zini:
There we go ...
--- Code: ---listCandidates (file):
e = extension (file)
if e in header_extension_list:
extension_list = source_extension_list
elif e in source_extension_list:
extension_list = header_extension_list
else:
raise error (unknown extension)
l = leaf_without_extension (file)
for f in project: # iterate over all files in the project
if leaf_without_extension (f)==l and extension (f) in extension_list:
add f to list
return list
findCandidate (file, list):
if not strictMode and len (list)==1:
return list[0]
n = len (file) # len (file) is the number of path elements
if n>1: # search for shared path elements
for f in list if len (f)==n:
for i in range (n-1): # for loop over the first n-1 integers starting with 0
if f[i]==file[i]:
return f # success
else # name consists of leaf elements only
for f in list if len (f)==1:
return f # success (top level directory)
# maybe add other methods to determine the file in case the above fails
return null # no file found; either does match no known pattern or it does not exist
swap (file):
list = listCandidates (file)
c = findCandidate (file, list):
if c!=null:
open c
return
# file does not exist -> try to find another file pair
parent = file[0:n-2] # one level up (the directory where the file is located)
for f in parent if f!=file: # iterating over the files in the directory
c = findCandidate (f, list)
if c!=null:
# may want to include a sanity check here: warn or abort if file already exists
create corresponding file in directory c[0:n-2] and open it
return
# give up; either ask the user for a directory or report that the file can't be created
--- End code ---
Optimized, better documented and with a little bug fix. This should work.
Regarding the strictMode variable: This should be either a Code.:Blocks-wide or a project-wide setting. In strict mode the algorithm will not handle some less symmetric source layouts correctly. In non-strict mode multiple files with the same name won't be handles correctly, if one of the files still needs to be created. I think that is a good compromise.
Edit: File names are assumed to be relative to the project directory.
ironhead:
Cool, thank you for the detailed pseudo code. I'll take a look at implementing it next week (I have a couple of deadlines to take care of this week).
Cheers!
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version