Posted 7 years ago by Montasser Ben Ouhida - Lead Software Engineering, Integration Objects
Version: 12.1.0300
Avatar

Hi all,

I would like to highlight the all codes like Visual Studio. The default types ( "string", "bool", "int", ...) are highlighting on blue, but the .Net types ("String", "ArrayList", ... ) still in black.

I use this instruction to use the visual studio render.

 syntaxEditor.Renderer = new VisualStudio2005SyntaxEditorRenderer();

When I use the syntax editor with C# code like this example:

// TODO 
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Value", typeof(String)));
dt.Columns.Add(new DataColumn("Value2", typeof(String)));
dt.Rows.Add(new object[]{"V1", "V11"});
dt.Rows.Add(new object[]{"V2", "V22"});

The texts DataTable,  DataColumn, String are in black but I want them in light green.

Comments (8)

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Montasser,

I'm sorry but the add-on doesn't currently support the feature of syntax highlighting recognized types.  We have it on the TODO list but It's actually a very difficult feature to implement.


Actipro Software Support

Posted 7 years ago by Tobias Lingemann - Software Devolpment Engineer, Vector Informatik GmbH
Avatar

fyi, I have tried the same, but the performance was not acceptable.

If you however want to try it anyway, this is what you need to do:

1. Add additional properties to the Token class (like IsKnownType or IsMethod)

2. If your semantic parse data is refreshed, you need to enrich the token (set the new properties IsKnownType and IsMethod).

I use the GetContextAtOffset method, which collects all needed data, but is very slow.

3. In the GetTokenHighlightingStyle method of your language you need to return the correct style depending on the new properties

See http://www.actiprosoftware.com/community/thread/6200/semantic-syntax-highlighting for more.

 

This works for small documents and fast machines, but since you cannot work with tokens in a different thread, you dont have many options here.

The correct way would be to make a symbol database like Visual Studio does and then use it instead of calling the GetContextAtOffset method, but this requires much more effort.


Best regards, Tobias Lingemann.

Posted 7 years ago by Montasser Ben Ouhida - Lead Software Engineering, Integration Objects
Avatar

Hi Tobias,

Thank you for this answer. I will try to implement the Token class. If I have any accepted result. I will replay the solution.

 

Best regards.

Posted 7 years ago by Tobias Lingemann - Software Devolpment Engineer, Vector Informatik GmbH
Avatar

Oh, I totally forgot one thing. I don't know if you already got that from my steps, but you need the sources of the AddOn. So you need to build the assembly yourself. You cannot do that with the default assemblies.


Best regards, Tobias Lingemann.

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

On a side note, this sort of feature may work better in the WPF/Silverlight SyntaxEditor design since there you can scan tokens in a worker thread.  Also, you have easy access to run a resolver on any offset, so it will tell you if an identifier is a namespace, type, method, etc.  And there you have the ability to dynamically "tag" regions of text as needing a different foreground via a classification tag when you do find that an identifier is a type.


Actipro Software Support

Posted 7 years ago by Montasser Ben Ouhida - Lead Software Engineering, Integration Objects
Avatar

Hi all,

Finally, I found a solution but it's not the great one.

First, I create a dictionary containing all types (using reflection).

Then, I have a new language class that inherits from CSharpSyntaxLanguage class and overrides the method GetHighlightingStyle.

In this method, I check if the token is unknown (simple instruction and I test if the corresponding word is a type.

The class code:

    /// <summary>
    /// Represents a C# language definition.
    /// </summary>
    internal class MyCSharpSyntaxLanguage : CSharpSyntaxLanguage
    {
        private SyntaxEditor syntaxEditor = null;

        public MyCSharpSyntaxLanguage(SyntaxEditor syntaxEditor)
            : base()
        {
            this.syntaxEditor = syntaxEditor;
            HighlightingStyle hTypes = new HighlightingStyle("DefinedType",
                         "DefinedType", Color.FromArgb(43, 165, 216), Color.White);
            this.HighlightingStyles.Add(hTypes);
           
        }
               

        public override HighlightingStyle GetHighlightingStyle(IToken token)
        {
            // Check the token type
            if (token.ID == CSharpTokenID.Identifier)
            {
                String strWord = syntaxEditor.Document.GetTokenText(token);
                if (MyManager.DotNetTypes.ContainsKey(strWord))
                    return HighlightingStyles["DefinedType"];
            }
            return base.GetHighlightingStyle(token);
        }
    }   

Best regards,

Montasser

Posted 7 years ago by Tobias Lingemann - Software Devolpment Engineer, Vector Informatik GmbH
Avatar

Well, the dictionary still needs to be updated to contain all code files of your project. And you currently cannot distinguish between namespaces and types. For example the full qualified name would be "Company.Tool.Component.Component".

Basically you cannot do it right without semantic parser information that is regulary updated.


Best regards, Tobias Lingemann.

Posted 5 years ago by Varun Chopra
Avatar

Hi all,

the solution given by Montasser stands correct. I just want to suggest a few more changes.

/// <summary>
/// Get the Token Text for the currently opened Code Editor Window. Based on the Text Range 
/// represented by Token
/// </summary>
/// <remarks>Returns the Token Text by searching the text range corresponding currentSyntaxEditor /// </remarks>
/// name="token"></param>
/// <returns></returns>
private static string GetTokenText(IToken token)
{
  string tokenText = string.Empty;
  if (!isCurrentSyntaxEditorNull && CurrentSyntaxEditor.Text != null && token != null 
       && token.Length > 0)
  {
    tokenText = CurrentSyntaxEditor.Text.Replace("\r\n", "\n").Substring(token.StartOffset, 
                                                                          token.Length);
  }
  return tokenText;
}
The latest build of this product (v2018.1 build 0341) was released 3 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.