Monday, December 11, 2017

#1 2009-10-29 03:17:36 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Commenting Code

In the Xcode Applescript  menu there is a sub menu comments.

It is possible to un/comment a selected section of Objective-C code from this menu.

I thought it would be a simple matter of duplicating this Perl script and changing the reference to // with -- and it would work with ASOC.

It does not.

It does work if you have Objective-C code in the ASOC script using -- instead of //.

Not being competent enough in Perl, does anyone have a working version they would be willing to share?


All the best

Terry

Offline

 

#2 2009-10-29 08:06:23 am

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: Commenting Code

You can put this in the Xcode scripts for commenting, I did:

Commenter:

Applescript:

tell application "Xcode"
   set contents of selection to ("(*" & (contents of selection) & "*)")
end tell

De-commenter:

Applescript:

set valdia to button returned of (display dialog "Would you like to remove (* *)s or --s or all comment script?" buttons {"(* *)", "--", "All"} default button 3 with title "De-Commenter")
tell application "Xcode"
   set valtxt to contents of selection
   if valdia is "(* *)" then
       set AppleScript's text item delimiters to "(*"
       set the valtxt to every text item of valtxt
       set AppleScript's text item delimiters to ""
       set valtxt to the valtxt as string
       set AppleScript's text item delimiters to "*)"
       set the valtxt to every text item of valtxt
       set AppleScript's text item delimiters to ""
       set valtxt to the valtxt as string
   else if valdia is "--" then
       set AppleScript's text item delimiters to "--"
       set the valtxt to every text item of valtxt
       set AppleScript's text item delimiters to ""
       set valtxt to the valtxt as string
   else
       set AppleScript's text item delimiters to "(*"
       set the valtxt to every text item of valtxt
       set AppleScript's text item delimiters to ""
       set valtxt to the valtxt as string
       set AppleScript's text item delimiters to "*)"
       set the valtxt to every text item of valtxt
       set AppleScript's text item delimiters to ""
       set valtxt to the valtxt as string
       set AppleScript's text item delimiters to "--"
       set the valtxt to every text item of valtxt
       set AppleScript's text item delimiters to ""
       set valtxt to the valtxt as string
   end if
   set contents of selection to valtxt
end tell

Last edited by Richard (2009-10-29 12:20:14 pm)


Filed under: xcode

Offline

 

#3 2009-10-29 01:55:42 pm

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Commenting Code

Hi Richard,

Thanks for your suggestions. It got me thinking of Applescript instead of Perl.

I have come up with a simple un/comment script for ASOC which just uses "--" which is my preferred comment method as it allows you to trial and error individual lines of code.

The script removes "--" if the line starts with a comment or adds a comment to the start of the line if it is not commented.

Here it is. If you or others have suggestions then please post:

Applescript:

tell application "Xcode"
   set tOldCont to contents of selection
   set tNum to number of paragraphs in tOldCont
   set tNewCont to ""
   repeat with n from 1 to tNum
       set tPara to paragraph n of tOldCont
       if tPara starts with "--" then
           set oldDelims to AppleScript's text item delimiters
           set AppleScript's text item delimiters to "--"
           set tPara to every text item of tPara
           set AppleScript's text item delimiters to oldDelims
           if n < tNum then
               set tPara to (tPara as string) & return
           else
               set tPara to (tPara as string)
           end if
       else
           if n < tNum then
               set tPara to "--" & paragraph n of tOldCont & return
           else
               set tPara to "--" & paragraph n of tOldCont
           end if
       end if
       set tNewCont to tNewCont & tPara
   end repeat
   set contents of selection to tNewCont
end tell

All the best

--Terry

Last edited by tellboy (2009-10-29 01:56:52 pm)


Filed under: xcode

Offline

 

#4 2009-10-29 03:21:05 pm

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: Commenting Code

Hmmm To me that one is annoying because it opposites everything, say you wanted a section of script with a few comments entirely block commented with dashes. But the way you wrote that is very good, I can't say the same for mine it is a modified part of an ASE script folder script. wink

Last edited by Richard (2009-10-29 03:23:34 pm)

Offline

 

#5 2009-10-30 03:14:13 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Commenting Code

Hi,

I was trying to replicate the un/comment script for Objective-C which I have used at various times so it has become familiar.

However, I just noticed that the Objective-C version makes it's choice as to whether to comment or uncomment based upon the first line of the selection.

So, if the first line does not have a comment then comments are added to all lines in the selection. That means if another line in the selection has a comment it will now have two. I am not sure if that is really to be desired.

I think I will change my script so that the first line of the selection makes the choice as to whether to uncomment or comment then that choice is applied to all lines rather than the way it is at the moment.

What do you think?

All the best

--Terry

Offline

 

#6 2009-10-30 03:53:21 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Commenting Code

Hi again,

On second thoughts the Apple way seems to be correct. You can have multiple comments on the same line allowing you to uncomment selected sections but keeping individual lines still commented.


Here, is my script.If you see any errors please correct and repost.

Applescript:

tell application "Xcode"
   set tOldCont to contents of selection
   set tNum to number of paragraphs in tOldCont
   set tNewCont to ""
   set tPara to paragraph 1 of tOldCont
   if tPara starts with "--" then
       set tAddFlag to false
   else
       set tAddFlag to true
   end if
   
   -- THIS SECTION REMOVES COMMENTS
   if tAddFlag = false then
       repeat with n from 1 to tNum
           set tPara to paragraph n of tOldCont
           if tPara starts with "--" then
               set tPara to (characters 3 thru -1 of tPara) as string
           else
               set tPara to every text item of tPara
           end if
           if n < tNum then
               set tPara to (tPara as string) & return
           else
               set tPara to (tPara as string)
           end if
           set tNewCont to tNewCont & tPara
       end repeat
   end if
   
   --THIS SECTION ADDS COMMENTS
   if tAddFlag = true then
       repeat with n from 1 to tNum
           set tPara to paragraph n of tOldCont
           if tPara starts with "--" then
               set tPara to every text item of tPara
           end if
           if n < tNum then
               set tPara to "--" & paragraph n of tOldCont & return
           else
               set tPara to "--" & paragraph n of tOldCont
           end if
           set tNewCont to tNewCont & tPara
       end repeat
   end if
   
   set contents of selection to tNewCont
end tell

All the best


--Terry


Filed under: xcode

Offline

 

#7 2009-10-30 07:59:33 am

Richard
Member
From:: Herefordshire, UK
Registered: 2009-09-09
Posts: 529
Website

Re: Commenting Code

You could always just put it all in the if rather than flag sections to be run or not.

Offline

 

#8 2009-10-31 03:48:25 am

tellboy
Member
Registered: 2005-06-23
Posts: 152

Re: Commenting Code

Hi,

Famous last words, the following now seems to work correctly.

It now removes tabbing from the selection as that was interfering with the placement and removal of "--".

If anyone could provide some optimisations then I would be grateful.

Applescript:

tell application "Xcode"
   set tComment to "--"
   set tOldCont to contents of selection
   set tNum to number of paragraphs in tOldCont
   set tNewCont to ""
   
   --REMOVE ALL TABS FROM THE SELECTION
   set tCont to ""
   repeat with n from 1 to tNum
       set tPara to paragraph n of tOldCont
       set oldDelims to AppleScript's text item delimiters
       set AppleScript's text item delimiters to tab
       set tPara to every text item of tPara
       set AppleScript's text item delimiters to oldDelims
       if n = tNum then
           set tCont to tCont & tPara
       else
           set tCont to tCont & tPara & return
       end if
   end repeat
   
   --SET WHETHER OR NOT TO ADD A COMMENT FLAG
   set tPara to paragraph 1 of tCont
   set tAddComment to true
   if tPara starts with "--" then
       set tAddComment to false
   end if
   
   --REPEAT THROUGH EACH LINE OF THE SELECTION
   repeat with n from 1 to tNum
       set tPara to paragraph n of tCont
       if n < tNum then
           if tAddComment is true then
               set tOutputString to tComment & tPara & return
           else
               if tPara starts with "--" then
                   set tOutputString to (characters 3 thru -1 of tPara) & return
               else
                   set tOutputString to tPara & return
               end if
           end if
       else
           if tAddComment is true then
               set tOutputString to tComment & tPara
               if tOutputString = "--" then
                   set tOutputString to ""
               end if
           else
               if tPara starts with "--" then
                   set tOutputString to (characters 3 thru -1 of tPara)
               else
                   set tOutputString to tPara
               end if
           end if
       end if
       set tNewCont to tNewCont & tOutputString
   end repeat
   
   --SET THE SELECTION
   set contents of selection to tNewCont
end tell

All the best

--Terry


Filed under: xcode

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)