0

Today, I migrate a Java program to VB.NET and, using Visual Studio 2019, I migrate following code

Static XMsg MSG_1001_CMD_PARAMETER_IS_NULL          = new XMsg(1001, True , "[%s] command's parameter [%s] cannot be null");
Static XMsg MSG_1002_CMD_PARAMETER_IS_EMPTY         = new XMsg(1002, True , "[%s] command's parameter [%s] cannot be empty");
Static XMsg MSG_1003_ERROR_WHEN_OPENING_OUPUT_FILE  = new XMsg(1003, True , "Error when opening OUTPUT file [%s]");
Static XMsg MSG_1004_ORACLE_ERROR_ON_ALTER_SESSION  = new XMsg(1004, False, "Oracle error when executing ALTER SESSION");

The Java code is formatted so that each parameter in new XMsg is formatted as a table. Each parameter value begin on same column.

When I transcode this Java code to VB.NET, I obtain following code

Shared MSG_1001_CMD_PARAMETER_IS_NULL = New XMsg(1001, True, "%", "[%s] command's parameter [%s] cannot be null")
Shared MSG_1002_CMD_PARAMETER_IS_EMPTY = New XMsg(1002, True, "%", "[%s] command's parameter [%s] cannot be empty")
Shared MSG_1003_ERROR_WHEN_OPENING_OUPUT_FILE = New XMsg(1003, True, "%", "Error when opening OUTPUT file [%s]")
Shared MSG_1004_ORACLE_ERROR_ON_ALTER_SESSION = New XMsg(1004, False, " ", "Oracle error when executing ALTER SESSION")

As you can see, XMsg parameters are not more correctly formatted because VB.Net make a formatting that delete duplicate spaces or tab characters each time I change a line.

Is there a tips allowing to keep basic formatting (keep spaces) ?

Using a special directive to declare that formatting is OFF ?

Using special Unicode character to replace space character ?

Using a specific code formatter program ? Which ?

schlebe
  • 3,387
  • 5
  • 37
  • 50
  • https://stackoverflow.com/q/737222/11683? – GSerg Jun 16 '20 at 19:06
  • @Gserg: my question is relative to VB.Net. Only VB.Net supress duplicate spaces when formatting. – schlebe Jun 16 '20 at 19:14
  • 1
    VB.Net settings are also considered there. – Jimi Jun 16 '20 at 19:21
  • Yes but stopping totally formatting is not an acceptable solution for me – schlebe Jun 16 '20 at 19:34
  • I believe you can turn off auto-formatting long enough to format the lines in question, then turn it back on. The editor typically doesn't mess with lines you aren't actively editing. – Darryl Jun 16 '20 at 20:23
  • Stopping formatting is certainly an answer, but it is certainly not the best answer to my question. – schlebe Jun 16 '20 at 20:27
  • No, there is no good way to do that in Visual Studio. If you need that feature consider a different editor. – Sam Axe Jun 16 '20 at 20:48
  • 2
    *"it is certainly not the best answer to my question"*. It is the best answer because there is nothing that will format some code and not other. The fact that it's not the answer you want doesn't mean that it's not the best answer you're going to get. If you want to pay for a tool like ReSharper then you may be able to do what you want but VS won't do exactly what you want and I'm not aware of any other extension that will either. You're free to look for such a tool but requests for third-party tools is one of the specific reasons for closing questions on SO, so it's not valid question here. – jmcilhinney Jun 17 '20 at 00:15
  • @jmcilhinney: are you sure that request to third-party tool close question ? Example: https://stackoverflow.com/questions/360282/what-are-the-best-java-code-generation-tools-or-plugins-to-use-in-eclipse – schlebe Jun 17 '20 at 16:22
  • That someone else posts a bad question does not mean that it's OK. One of the specific reasons available for closing a question is *"Seeking recommendations for books, tools, software libraries, and more"* so yes, I'm sure. – jmcilhinney Jun 18 '20 at 00:01

1 Answers1

0

Until VB.Net doesn't define specific directives to stop and start VB.Net code formatting, it is impossible to align similar lines as in a table.

Since what I search is that messages of application are easily readable, I have written a little Text Template (T4 tools in Visual Studio 2019) that automatically generates my VB.NET code.

The VB.Net code is always NOT ALIGNED; but .TXT source file that is used by T4 tools to generate VB.NET code is aligned.

.TXT file that contains all messages is following

+----------------------------------------+------+------------------------------------------------------------------------------------------
| CMD_PARAMETER_IS_NULL                  | 1001 | [%s] command's parameter [%s] cannot be null
| CMD_PARAMETER_IS_EMPTY                 | 1002 | [%s] command's parameter [%s] cannot be empty
| ERROR_WHEN_OPENING_OUPUT_FILE          | 1003 | Error when opening OUTPUT file [%s]
| ORACLE_ERROR_ON_ALTER_SESSION          | 1004 | Oracle error when executing ALTER SESSION
| ORACLE_ERROR_IN_OPEN_DB                | 1005 | Oracle error in OPEN-DB= new XMsg()
| ZIP_ERROR_ON_CALC_CHAIN                | 1006 | ZIP error on CALC-CHAIN

.TT file that generates VB.NET code is following

<#@ template debug="false" hostspecific="false" language="VB" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".vb" #>

Public Partial Class M

<#
    Dim iPos = 1
    Dim sLine As String
    Dim sVariableName As String = "?"
    Dim iVariableNamePos As Integer
    Dim iVariableNameLen As Integer
    Dim sMsgNo As Integer
    Dim iMsgNoPos As Integer
    Dim iMsgNoLen As Integer
    Dim sNrVariables As String
    Dim sMessage As String = "?"
    Dim iMessagePos As Integer
    Dim bColumnPosInitialized = False

    Dim sr as StreamReader = New StreamReader(".\Sources\Messages.txt", Encoding.Default)

    Do While True
        sLine = sr.ReadLine
        If sLine is Nothing then
            Exit Do
        End If

        If sLine.StartsWith("+") Then
            If Not bColumnPosInitialized Then
                bColumnPosInitialized = True
                iVariableNamePos = sLine.IndexOf("+") + 2
                iMsgNoPos = sLine.IndexOf("+", iVariableNamePos + 1) + 2
                iMessagePos = sLine.IndexOf("+", iMsgNoPos + 1) + 2
                iVariableNameLen = iMsgNoPos - iVariableNamePos - 3
                iMsgNoLen = iMessagePos - iMsgNoPos - 3
            End If
        Else
            If sLine.StartsWith("|") Then
                sLine &= "  "
                sNrVariables = "0"
                sVariableName = sLine.Substring(iVariableNamePos, iVariableNameLen).Trim
                sMsgNo = sLine.Substring(iMsgNoPos, iMsgNoLen).Trim
                sMessage = sLine.Substring(iMessagePos).Trim
                If sMessage.Contains("[%s]") Then
                    sNrVariables = "1"
                End If
#>
Shared <#= "MSG_" & sMsgNo & "_" & sVariableName #> = New XMsg(<#= sMsgNo #>, <#= sNrVariables #>, "%", "<#= sMessage #>")
<#
            End If
        End If
    Loop

    sr.Close()
#>

End Class

and generated VB.Net source code is following

Public Partial Class M

    Shared MSG_1001_CMD_PARAMETER_IS_NULL = New XMsg(1001, 1, "%", "[%s] command's parameter [%s] cannot be null")
    Shared MSG_1002_CMD_PARAMETER_IS_EMPTY = New XMsg(1002, 1, "%", "[%s] command's parameter [%s] cannot be empty")
    Shared MSG_1003_ERROR_WHEN_OPENING_OUPUT_FILE = New XMsg(1003, 1, "%", "Error when opening OUTPUT file [%s]")
    Shared MSG_1004_ORACLE_ERROR_ON_ALTER_SESSION = New XMsg(1004, 0, "%", "Oracle error when executing ALTER SESSION")
    Shared MSG_1005_ORACLE_ERROR_IN_OPEN_DB = New XMsg(1005, 0, "%", "Oracle error in OPEN-DB= new XMsg()")
    Shared MSG_1006_ZIP_ERROR_ON_CALC_CHAIN = New XMsg(1006, 0, "%", "ZIP error on CALC-CHAIN")
End Class

The .TT source file is currently a propotyp without syntax check and some limitation. But this is another story.

schlebe
  • 3,387
  • 5
  • 37
  • 50