Skip to content

Script Execution

Oleg Shilo edited this page May 10, 2017 · 5 revisions

The plugin allows the usual C# project management tasks to be performed in the way very similar to the MS Visual Studio. In addition to this it provides minor debugging functionality.

Typically user opens the C# file with Notepad++ and then presses 'Load' button on the CS-Script toolbar. After that the all features can be accessed through three Notepad++ dockable panels: Project/CodeMap, Debug and Output panels.

Features

Project panel

  • Create and load new script

  • Validate currently loaded script. It is an equivalent of the VS Build command. It can also be invoked via Ctrl+Shift+B shortcut.

  • Execute currently loaded script. It is an equivalent of the VS Start Without Debugging command. It can also be invoked via F5 shortcut. If the Notepad++ current .cs document is not loaded as script then plugin automatically loads it.

  • Start currently loaded script and trigger Assertion to allow attaching the system default CLR debugger (e.g. Visual Studio)

Code Map Panel

  • Display list of all script methods and properties

Debug Panel

  • Debug Panel provides multiple views (tabs) for assisting with the debugging scripts. Practically all debugging features are inspired by similar functionality of the Visual Studio.

Output Panel

  • Selection of the output source

  • Interception of the Console output of the executing script into 'Console' output panel. If the 'Intercept StdOut' is checked the console window of the script being executed will be hidden and all the Standard Outputstream content will be redirected to the output panel. Note that this does not affect neither Standard Input nor Error Output streams.

    Be careful when using this feature for the scripts using Console.ReadKey or Console.ReadLine methods. Without console window visible it is impossible to make Console input thus it will affect the runtime behavior of your script.

  • Displaying the Debug Output stream into 'Debug' output panel. Strictly speaking it is not a CS-Script specific feature but a system wide listener of the Debug Output. In a way it is similar to the well known DbgView.exe from Sysinternals. The first 4 digits of each raw are indicating the ID if the process writing to the Debug Output.

  • Filtering out all debug messages, which do not belong to the script being executed. In this mode displaying the process ID is suppressed.

  • At any time user can trigger displaying the list of all CS-Script shortcuts by clicking the corresponding button on the Project Panel toolbar.

  • If your want to ensure that all script Unicode output is displayed correctly in the console window ensure that the console has an appropriate font settings:

C# Project Model

The plugin's C# file project/execution model is based on the CS-Script C# source code model. A single .cs file forms a single-file C# project. The Notepad++ current document can be loaded as a script and the corresponding project structure generated by pressing Load button.

The referenced assemblies are resolved automatically from the using <namespace>; clauses. The CS-Script //css_ref (or //css_reference) directives can be used to reference assemblies explicitly. Extra C# files can also be added to the logical C# project with the //css_inc (or //css_include) directives. The information about all CS-Script directives (~8 in total) can be found here: http://www.csscript.net/help/Directives.html. The majority of the dependency scenarios can be handled by just two directives: //css_inc and //css_ref. And in many case there is even no need for any CS-Script directives at all as a plain vanilla C# file is usually compatible with the CS-Script Intellisense model.
The following are the samples of how to use the CS-Script directives: Including the C# file math.cs containing the Math class definition

//css_inc math.cs;
using System;
 
class Script 
{
    static public void Main(string [] args)
    {
        Console.WriteLine(Math.Calculator.Add(1,2));
    }
}

Referencing the math.dll assembly containing the Math class implementation

//css_ref math.dll;
using System;

class Script 
{
    static public void Main(string [] args)
    {
        Console.WriteLine(Math.Calculator.Add(1,2));
    }
}

NOTE: All CS-Script directives and using clauses are processed on the file reloading. Thus if the new directive or 'using' clauses added to the "current document" you may want to re-generate the project structure with the Reload button.

Classless scripts

One of the CS-Script directives //css_args /ac allows execution of the scripts without the script class defined. Such classless scripts sometimes may be practical to use as they provide more convenient code structure and yet they are fully supported by Notepad++ Intellisense. Also the latest script engine version distributed with Notepad++ plugin allows non-static entry points:

//css_inc math.cs;
using System;

void main(string [] args)
{
    Console.WriteLine(Math.Calculator.Add(1,2));
}

The acceptable entry point signatures are:

 void main() 
 int main() 
 int main(string[] args) 
 void main(string[] args) 

Elevated scripts

There is one script directive, which is not part of the CS-Script directives. It is //css_npp asadmin. It is a simple pure Notepad++ directive, which forces the script to be executed with the elevated privileges.