Format document in X# 2.21

This forum is meant for questions and discussions about the X# language and tools
JohnBonnett88
Posts: 57
Joined: Mon Mar 07, 2022 12:34 am

Format document in X# 2.21

Post by JohnBonnett88 »

Hi All,

In older versions of X# the Ctrl+K Ctrl+D Edit/Advanced option in VS would nicely format your X# code, just as it does in C#. Microsoft broke that with some changes but I see in the release notes that it is fixed again in X# 2.21.

I have tried it out and it does do things again but not perhaps what we need, so I have some examples.

I tried it on this code which was mostly created by the default X# WinForms project I used to make a baby app to fiddle with CRC calculations

Code: Select all

USING System
USING System.Collections.Generic
USING System.ComponentModel
USING System.Data
USING System.Drawing
USING System.Linq

USING System.Text
USING System.Threading.Tasks

USING System.Windows.Forms

BEGIN NAMESPACE test2

    PUBLIC PARTIAL CLASS Form1 ;
        INHERIT System.Windows.Forms.Form

        PUBLIC CONSTRUCTOR()   STRICT//Form1
            InitializeComponent()
            RETURN
        END CONSTRUCTOR
        
        PRIVATE METHOD btnCalc_Click(sender AS System.Object, e AS System.EventArgs) AS VOID STRICT
            LOCAL cDecCRC AS STRING
            LOCAL cHexCRC AS STRING
            
            cDecCRC := CalcCRCMess(txtPacket:Text)
            cHexCRC := Convert.ToString(Convert.ToInt32(cDecCRC), 16)
            lblCRC:Text := "Dec " + cDecCRC + " Hex " + cHexCRC
            RETURN
        END METHOD
            
    END CLASS 
END NAMESPACE
I tried Ctrl-K Ctrl-D on the above and got the result below.

Code: Select all

USING System
USING System.Collections.Generic
USING System.ComponentModel
USING System.Data
USING System.Drawing
USING System.Linq

USING System.Text
USING System.Threading.Tasks

USING System.Windows.Forms

BEGIN NAMESPACE test2
    
PUBLIC PARTIAL CLASS Form1 ;
        INHERIT System.Windows.Forms.Form
    
    PUBLIC CONSTRUCTOR()   STRICT//Form1
        InitializeComponent()
        RETURN
    END CONSTRUCTOR
    
    PRIVATE METHOD btnCalc_Click(sender AS System.Object, e AS System.EventArgs) AS VOID STRICT
        LOCAL cDecCRC AS STRING
        LOCAL cHexCRC AS STRING
        
        cDecCRC := CalcCRCMess(txtPacket:Text)
        cHexCRC := Convert.ToString(Convert.ToInt32(cDecCRC), 16)
        lblCRC:Text := "Dec " + cDecCRC + " Hex " + cHexCRC
        RETURN
    END METHOD
    
END CLASS 
END NAMESPACE
It has aligned the NAMESPACE and CLASS blocks and left the rest indented.

A more worrying one is this code fragment.

Code: Select all

	IF dwTotal > 0
		FOR dwN := 1 UPTO dwTotal			
			cFile := aFileList[dwN,1]
			SELF:oDCStatusSLE:TextValue := "Processing " +  cFile + "         "
			ApplicationExec(EXECWHILEEVENT)
			//PrintStr("process file", cFile)
			SELF:ProcessXML(cFile)		
		NEXT
	ENDIF
	SELF:oDCStatusSLE:TextValue := "Waiting                           "
	
	SELF:oDCTimerTxt:Start()

	RETURN NIL
Using the Format Selection option Ctrl-K Ctrl-F, I got the following:

Code: Select all

IF dwTotal > 0
FOR dwN := 1 UPTO dwTotal			
cFile := aFileList[dwN,1]
SELF:oDCStatusSLE:TextValue := "Processing " +  cFile + "         "
ApplicationExec(EXECWHILEEVENT)
//PrintStr("process file", cFile)
SELF:ProcessXML(cFile)		
NEXT
ENDIF
SELF:oDCStatusSLE:TextValue := "Waiting                           "

SELF:oDCTimerTxt:Start()

RETURN NIL
It has left justified everything. Ctrl+K Ctrl+D left justifies the whole document. I am not sure why this code behaves differently than my first example. The indents on the original code fragment here used TAB characters whereas the previous example use 4 spaces. I tried changing that on the second example but got the same result.

I thought I should warn people to not use this editing option unless they like left justified code. I don't know where the rules of indenting get defined for this but perhaps Robert knows. I often use these options in C# when I move code about to get things tidy again. It would be good if it worked in X# too.

Cheers,
John Bonnett
User avatar
Chris
Posts: 5468
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Format document in X# 2.21

Post by Chris »

Hi John,

Both seem to work fine here in 2.22.

For the indenting rules, you need to go to the VS options -> Text Editor -> X# -> Indentation and there you can enable indentation of namespace members.

For the other code snippet, maybe it's something else above that code that causes the problem. Please show the complete code of the prg, so we can give it a try with that, too.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
ic2
Posts: 1956
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Re: Format document in X# 2.21

Post by ic2 »

Hello Chris,

I haven't installed the last few X# updates. According to Kees no new errors came up with these versions in his project so I downloaded and installed 2.22 and indeed it all complies without further issues, which is great!

I can confirm that the code John mailed indents correctly with Ctrl K Ctrl D, but there's one remarkable thing: it takes a full 5 seconds before it actually indents!

Is that what you maybe get too John?

Although huge steps are made with Intellisense throughout the versions VO is still better in that field. Whenever I have to work in X# I often type my code in VO and then paste it in VS. For example, I still hate it that typing a closing tag like ENDIF only alligns with IF when I press <Enter>, so I have to delete the empty line which is a result of the unwanted press of <Enter>. One of the reasons I still edit most code, including X# code, in VO.

Dick
User avatar
Chris
Posts: 5468
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Format document in X# 2.21

Post by Chris »

Hi Dick,

Is this a VERY large prg file?
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
ic2
Posts: 1956
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Re: Format document in X# 2.21

Post by ic2 »

Hello Chris,

Not sure where I pasted it in, but after reading your reply I realized that Ctrl K - Ctrl D formats the whole document (Advanced, Format Document) while Ctrl K Ctrl F formats only the selection.

The D in Ctrl (K+) D is from Document I guess and the the F in Ctrl (K+) F from...Felection I guess) VS has many very logical keystrokes.

It is quite slow, but that is with almost everything in VS. I opened a prg with 1200 lines where Ctrl K Ctrl D takes indeed 5 seconds (I opened an MEF in VO of 1800 lines and Indent takes less than 2 seconds).

But it might explain what John wrote if he tried Ctrl K Ctrl D in a large program.

Dick
User avatar
robert
Posts: 4884
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Re: Format document in X# 2.21

Post by robert »

Dick,
The keyboard shortcuts in VS can be completely configured and depend on the setting that you have chosen when you first started working in VS.
You can change the default from Tools / Import and Export Settings.
Choose "Reset Settings", Next, Next. You can then choose from several options, like "General", "JavaScript", "Visual Basic" and "Visual C#".
Each of these has its own set of shortcut keys.
I have C# settings, and they have Ctrl-E - D for format document.
To adjust individual commands choose Tools / Customize and press on the Keyboard button.
So you can assign the Ctrl-K-S to format selection if you want.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
ic2
Posts: 1956
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Re: Format document in X# 2.21

Post by ic2 »

Hello Robert,

I know I can configure the shortcuts. Drawback is that more than once you'll find an answer on a question with the (default) shortcut and not the exact menu position. This then won't work when another combination was assigned. And also if I would work with someone's VS who did not assign shortcuts it is an advantage if (the few) keystrokes I know by heart actually work.

I just try to understand why so few shortcuts with a letter in it deviate so often from the actual command description in the default setting. But I failed to do so.

There's one thing I would be willing to change, which is commenting/uncommenting with a simple one key toggle, like Ctrl B in VO. Unfortunately, that is not possible. Nobody at Microsoft was smart enough to see that a toggle is the only logical way.

Instead of Ctrl K Ctrl C , Ctrl K Ctrl U....

Dick
JohnBonnett88
Posts: 57
Joined: Mon Mar 07, 2022 12:34 am

Re: Format document in X# 2.21

Post by JohnBonnett88 »

In reply to Dick:

I also had noticed it sometimes takes a while when you use Ctrl+K Ctrl+D but that is rather expected if it is a big document. I sometimes also get messages from VS saying that is not responding, it believes the X# extension is causing the problem and perhaps I should remove it!

In reply to Chris:

I will have a play and see if I can identify the feature of the file that seems to be encouraging it to left justify everything. Even if you just format a selection I guess it has to look at the lines above that to figure out what the right indentation would be. I will also see what I can achieve by playing with the VS options.

Thanks for the feedback,
John
VR
Posts: 113
Joined: Sun Aug 23, 2020 3:07 pm
Location: Italy

Re: Format document in X# 2.21

Post by VR »

ic2 wrote: Thu Feb 06, 2025 9:35 pm There's one thing I would be willing to change, which is commenting/uncommenting with a simple one key toggle, like Ctrl B in VO. Unfortunately, that is not possible. Nobody at Microsoft was smart enough to see that a toggle is the only logical way.

Instead of Ctrl K Ctrl C , Ctrl K Ctrl U....
Vistual Studio 2022 (not sure about 2019) support toggleing single line and block comments. You can find the shortcuts in Edit > Advanced > Toggle line comment

Volkmar
ic2
Posts: 1956
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Re: Format document in X# 2.21

Post by ic2 »

Hello Volkmar,
VR wrote: Sun Feb 09, 2025 4:33 pm You can find the shortcuts in Edit > Advanced > Toggle line comment
Indeed - also in VS 2019. I have commented on this this before and you are the first to point me to this option.
It does make me wonder what the use is of separate Comment/Uncomment commands (unless you want to comment out already commented out code) and why these 2 options are in the main screen default toolbar and the Ctrl K Ctrl \ for the toggle isn't. But for this I'll make an exception and assign Ctrl B (which is also a toggle I saw, but this adds /* and */ ).

Thanks!

Dick
Post Reply