Home Download Buy Blog Forum Support

Removing Comments from String

Removing Comments from String

Postby ambs on Sun Dec 08, 2013 3:27 pm

Hello

In a plugin I am trying to develop, I managed to extract a block of code (I have it both as a region or a string).

I would like to remove comments from that string using the active syntax (whatever it is) and obtain the string from that block of code uncommented.
I do not want to replace anything in the buffer/view, just trying to get that string intact, without the comments characters.

Is there a way to do that without implementing it myself, or without duplicating existing code from the comment/uncomment feature already available?

Thank you for your help,
Best,
Alberto
ambs
 
Posts: 5
Joined: Thu Nov 28, 2013 7:52 pm

Re: Removing Comments from String

Postby sapphirehamster on Sun Dec 08, 2013 9:21 pm

There are a variety of different ways you can approach it. A relatively simple way would be to toggle and then untoggle the comment, like this:

Code: Select all
        self.view.run_command('toggle_comment')
        ss = map(self.view.substr, self.view.sel())
        self.view.run_command('toggle_comment')
        print('uncommented=%r' % list(ss))


This assumes the selection covers a commented area. You can refine the selection by doing something like:

Code: Select all
z = view.find_by_selector('comment')
for s in view.sel():
    commented_regions = filter(s.intersects, z)


This will have the unfortunate side effect of marking the view dirty. The cleanest approach would be to duplicate the code in the comment module and modify it to edit a string instead of the view. It would be maybe 100 lines of code. You don't have to copy everything (import it and reuse functions like build_comment_data).

Another idea, and this may be a bad one, is to temporarily put the string into a scratch buffer. This will avoid modifying the current view. Something like:
Code: Select all
        v = self.view.window().new_file()
        v.set_scratch(True)
        v.set_syntax_file(self.view.settings().get('syntax'))
        v.insert(edit, 0, self.view.substr(self.view.sel()[0]))
        v.sel().add(sublime.Region(0, v.size()))
        v.run_command('toggle_comment')
        s = v.substr(sublime.Region(0, v.size()))
        v.close()
        print('uncommented=%r' % s)


Again this assumes the current selection is commented, you would need to filter it if desired.

Anyways, I hope this gives you some ideas. Personally, I wouldn't be afraid of duplicating some of the logic in comment.py.
sapphirehamster
 
Posts: 83
Joined: Sun Jul 01, 2012 11:19 pm

Re: Removing Comments from String

Postby ambs on Mon Dec 09, 2013 5:06 pm

Is there a way to do that without implementing it myself, or without duplicating existing code from the comment/uncomment feature already available?


Hello
Thank you for the heads up. When you are new to some tool/language/whatever you are always afraid you are going the wrong way.
So, thank you for your suggestions. Yes, I think I'll go with the copying/duplicating code from comment.py.

Thank you so much,
Alberto
ambs
 
Posts: 5
Joined: Thu Nov 28, 2013 7:52 pm


Return to Plugin Development

Who is online

Users browsing this forum: Exabot [Bot] and 6 guests