Join  |  Sign in
Editing of code snippets

14-Jul-2017 01:29 AM

Wolfgang HG

Wolfgang HG

Posts: 21

Hi,

just evaluating the control - looks quite promising.

But we have a special use case, and I did not find anything whether you support this:
Our application provides several scripting frameworks (VB.NET). Each of those scripts consists just of a method body.
When executing the script, we generate the class declaration (including a base class inheritance) and the method declaration (which implements actually an abstract base class method), insert the method body snippet and compile it to an assembly.

Sample:
public class MyClass inherits MyBaseClass
  public overrides function MyFunc (...params...)  as String
    ...here the snippet is placed...
 end Function
End Class


The user should edit only the snippet inside the method, and should not see the rest. But of course the editor control should still be able to perform full validation.

So, I would assume that we provide some "before/after" code, and your control just displays the method body.

Is this possible?

A workaround migt be to show the full code, but make all non-editable rows readonly.

Best regards

Wolfgang
  Reply

14-Jul-2017 09:06 PM

Andrew Medvedev

Andrew Medvedev

Posts: 49

Hi Wolfgang,
We have two Visual Basic parsers in the package based on Microsoft Code Compiler technology ("Roslyn"), located in Alternet.Syntax.Parsers.Roslyn and our hand-written one in Alternet.Syntax.Parsers.Advanced assembly.

It should be possible to achieve method body editing only preserving all other features (code completion, syntax highlighting, code formatting, etc.) for both of them. I would recommend using Roslyn-based parser, as it's based on the same technology used in Visual Studio code editor.

With Roslyn-based parsers there will be some small modifications required to the source code.
Approach with readonly lines is also possible to implement. Please refer to this demo project for referfence (http://www.alternetsoft.com/projects/SnippetsParsers.zip)

Regards,
Andrew

  Reply

14-Jul-2017 09:42 PM

Wolfgang HG

Wolfgang HG

Posts: 21

Hi Andrew,

the sample does not compile for me (after changing the references to the local alter.NET DLLs).

One of the errors:
'SnippetVBParser.SnippetVBTokenizer.Symbols': cannot override inherited member 'RoslynParser.Tokenizer.Symbols' because it is not marked virtual, abstract, or override.

I assume you linked it agains your own source code, which has changed since release 1.1?

Best regards

Wolfgang
  Reply

15-Jul-2017 12:01 AM

Andrew Medvedev

Andrew Medvedev

Posts: 49

Hi Wolfgang,

Yes, we had to change the source code slightly for it to work. Please run SnippetsParsers.exe from the bin folder to see if it works the way you need. Changing reference to Alternet.Syntax.dll and Alternet.Syntax.Parsers.Roslyn.dll in the same folder should allow it to be compiled.

regards,
Andrew
  Reply

15-Jul-2017 01:39 AM

Wolfgang HG

Wolfgang HG

Posts: 21

Thanks, this looks perfect!

I will be on vacation for the next 6 weeks, and after this I will talk to my bosses and try to convince them to buy a license.

Wolfgang
  Reply

17-Jul-2017 12:08 AM

Andrew Medvedev

Andrew Medvedev

Posts: 49

Hi Wolfgang,

Glad it worked for you.

Fell free to write me at andrew.medvedev@alternetsoft.com should you have further questions.

regards,
Andrew

  Reply

25-Nov-2017 03:02 AM

Wolfgang HG

Wolfgang HG

Posts: 21

Hi,

I have a small problem with indent in the snippet sample:
I added a namespace declaration to the class in the "SnippetsParsers" sample (and increased the "StartMethodOffset" constant).

When I add press the enter key in editor, the new line is indented by one tab. I did not find the place to remove this. Increasing the tab count for "ExtraVbSpaceRemover" did not help.

I could post a sample, but it seems I cannot attach files here?

And one more question: it seems the VB parser does not set "Option explicit = true". How could this be done?

Best regards

Wolfgang
  Reply

27-Nov-2017 07:46 PM

Wolfgang HG

Wolfgang HG

Posts: 21

Here are the Code snippets necessary to reproduce the problem (it seems I cannot copy/paste when creating forum posts using Firefox, and today i tried it with Internet Explorer, where it works):

    public const string MethodSnippet = @"Imports System
Public Namespace MyNamespace
  Public Class MyClass1
    Public overrides Function MyFunc() As String
{0}
    End Function
  End Class
end Namespace";

    public const int StartMethodOffset = 4; //3;
    public const int EndMethodOffset = 3; //2;

Best regards

Wolfgang

  Reply

27-Nov-2017 09:08 PM

Wolfgang HG

Wolfgang HG

Posts: 21

I resolved my question "Option explicit on" myself: it is quite simple, just add one line to the MethodSnippet - and I actually wanted to set "Option strict on" ;-).

I found one more problem: the sample crashes with an ArgumentOutOfRangeException.

To reproduce: I uploaded a video: http://www.hg-online.de/downloads/alternet_codeeditor_exception.zip

1) Switch to "Visual Basic"
2) mark the last line of code, press the Backspace key to delete it.
3) press "Backspace" once again => the exception occurs.

System.ArgumentOutOfRangeException occurred
  HResult=0x80131502
  Message=Der Index und die Länge müssen sich auf eine Position in der Zeichenfolge beziehen.
Parametername: length
  Source=mscorlib
  StackTrace:
   at System.String.Substring(Int32 startIndex, Int32 length)
   at Alternet.Editor.Selection.SmartSetSelectedText(String text, SelectionType selType)
   at Alternet.Editor.Selection.UndoOperations(ITextUndoList operations, Boolean withUndo)
   at Alternet.Editor.Selection.SmartFormat(Boolean needFormat, Boolean notify, Boolean needIndent, Boolean jumptoEnd, Boolean extended, Int32 line)
   at Alternet.Editor.Selection.SmartFormat(Int32 line)
   at Alternet.Editor.SyntaxEdit.Notification(Object sender, EventArgs e)
   at Alternet.Editor.TextSource.TextSource.Notify(EventArgs e)
   at Alternet.Editor.TextSource.TextSource.Update()
   at Alternet.Editor.TextSource.TextSource.EndUpdate()
   at Alternet.Editor.Selection.DeleteLeft(Boolean deleteWord)
   at Alternet.Editor.Selection.DeleteLeft()
   at Alternet.Editor.EventHandlers.DeleteLeft()
   at Alternet.Editor.KeyList.ExecuteKey(Keys keys, Int32& state)
   at Alternet.Editor.SyntaxEdit.ProcessKey(Keys keyData)
   at Alternet.Editor.SyntaxEdit.OnKeyDown(KeyEventArgs e)
   at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
   at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
   at System.Windows.Forms.Control.WmKeyChar(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at Alternet.Editor.SyntaxEdit.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at SnippetsParsers.Program.Main() in C:\Temp\SnippetsParsers_WinForms\Program.cs:line 32

Best regards

Wolfgang

  Reply

29-Nov-2017 06:34 PM

Andrew Medvedev

Andrew Medvedev

Posts: 49

Hi Wolfgang,

I'm upload modified version of Snippet Parser, please let me know if you see problems with indentation.
http://www.alternetsoft.com/projects/VBSnippetParser.zip

We've also fixed issue with it crashing when deleting last line, the fix is going to be included in the upcoming update which is expected very shortly.
Could yo please also elaborate on Option explicit = true question, I'm not sure if I understood it correctly?

regards,
Andrew
  Reply

29-Nov-2017 07:48 PM

Wolfgang HG

Wolfgang HG

Posts: 21

Hi,

the new sample does not compile (override SetSourceText is not available). So I cannot verify this.
We also observed indentation issues in our own code snippets (also without namespace declaration). I will try to create a sample so that you can check whether it will work with your new version.

About "option explicit": this was a misunderstanding, I wanted to us "option strict on". I need this to mark things like this as errors:
dim i as Integer = "1"
=>no automatic conversion of types.
The resolution is to add a line "option explicit on" to the class code.

Best regards

Wolfgang
  Reply

29-Nov-2017 08:43 PM

Wolfgang HG

Wolfgang HG

Posts: 21

I uploaded a modified "SnippetParser" sample to http://www.hg-online.de/downloads/SnippetsParsers_Indenting.zip, which loads the snippets from three vb files (the snippet code itself does not compile, because it would require a lot of our custom dlls).

I modified the sample to use "syntaxEdit1.IndentOptions = IndentOptions.None" (and also tried "UsePrevIndent"), but when editing, still some indention occurs. See the video "indent.avi" in the file. It gets more strange when you edit inside the existing code.

Note that the snippet.vb code is indented quite chaotic - our previous code editor was just a textbox ;-).

The easiest solution would be to do no automatic indenting at all, so that the user has to do indenting himself by pressing TAB or SPACE. But though I think that I switched it off, indenting still happens.

Another solution would be if you could provide a method "PerformIndent" which formats an existing code after setting it to the control. I did not find such a method ;-). Afterwards, the control could do all automatic indenting when the user types.

Wolfgang

  Reply

01-Dec-2017 07:10 PM

Andrew Medvedev

Andrew Medvedev

Posts: 49

Hi Wolfgang,

With your example I've added checking for the SubBlock to the IsBlockNode, indentation is works better for me now.

        protected override bool IsBlockNode(Microsoft.CodeAnalysis.SyntaxNode node)
        {
            return base.IsBlockNode(node) && !(node.Kind() == SyntaxKind.FunctionBlock) && !(node.Kind() == SyntaxKind.SubBlock)  && !(node.Kind() == SyntaxKind.ClassBlock);
        }

If you still like to set indent options to none, you need to set it to the Source (or to the editor after Source is assigned), as IndentOptions is property of TextSource :

 vbSnippetSource.IndentOptions = IndentOptions.None;

Option strict can be set globally on a parser level. With the new version (which we expect to release very shortly), you will be able to do it using the following code:

        public VBSnippetRepository(ISyntaxParser owner, IRoslynSolution roslynSolution, bool caseSensitive, ISyntaxTree syntaxTree)
        : base(roslynSolution, caseSensitive, syntaxTree)
        {
            this.owner = owner;


            var solution = roslynSolution.Solution;
            var projectId = roslynSolution.ProjectId;
            var newSolution = solution.WithProjectCompilationOptions(projectId, new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optionStrict: OptionStrict.On));
            roslynSolution.Workspace.TryApplyChanges(newSolution);
        }

regards,
Andrew
  Reply

01-Dec-2017 07:45 PM

Wolfgang HG

Wolfgang HG

Posts: 21

Thanks, I will test it with the new version, and hopefully will find all snippets to add to the SnippetParser sample ;-).

Wolfgang
  Reply

06-Dec-2017 10:41 PM

Wolfgang HG

Wolfgang HG

Posts: 21

Hi Andrew,

I forgot about one unanswered question: is there any possiblity to apply formatting (identing) to the current editor?

Best regards

Wolfgang
  Reply

07-Dec-2017 07:24 PM

Andrew Medvedev

Andrew Medvedev

Posts: 49

Hi Wolfgang,

if you need to format all text with correct indentations (and apply other rules such as insert spaces between binary operators), you need to call Selection.SmartFormatDocument). This function is linked to Ctrl + K + D key keystroke.
Please let me know if this is not what you're after.

regards,
Andrew

  Reply

07-Dec-2017 07:29 PM

Wolfgang HG

Wolfgang HG

Posts: 21

Thanks, this sounds perfect.

It seems I have to add a context menu to our control ;-).
  Reply

13-Dec-2017 03:09 AM

Wolfgang HG

Wolfgang HG

Posts: 21

Just updated to 2.1. Seems my problems with identing and the crash are fixed. Many thanks!

But one additional small visual issue with the SnippetParser sample: take a look at the VB Code: it contains five lines which start with "Console." In the first three occurences, the class name is light blue, but occurence number 4 is black. Same in the C# snippet. 

This will probably be my last post before christmas, I am on vacation till then ;-)

Best regards

Wolfgang

  Reply

13-Dec-2017 08:32 PM

Andrew Medvedev

Andrew Medvedev

Posts: 49

Hi Wolfgang,

I have answered you via email.

regards,
Andrew
  Reply

13-Dec-2017 09:04 PM

Wolfgang HG

Wolfgang HG

Posts: 21

Just for the records: AlterNet sent me a modified sample which fixes this issue.
  Reply