From: Jean-Philippe Bruyère Date: Tue, 18 Feb 2020 15:52:31 +0000 (+0100) Subject: basic moving and selecting with SourceText as backend buffer, basic highlighting... X-Git-Tag: v0.9.5-beta~129 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=81a8401cfd8942ae06afc876ddd46e632922fd53;p=jp%2Fcrow.git basic moving and selecting with SourceText as backend buffer, basic highlighting with syntaxTree, save commit before using msbuild workspace --- diff --git a/Crow/src/Widgets/Border.cs b/Crow/src/Widgets/Border.cs index e5a16ec8..b3c72030 100644 --- a/Crow/src/Widgets/Border.cs +++ b/Crow/src/Widgets/Border.cs @@ -156,7 +156,7 @@ namespace Crow if (BorderStyle == BorderStyle.Normal) { if (BorderWidth > 0) { - Foreground.SetAsSource (gr, rBack); + Foreground?.SetAsSource (gr, rBack); CairoHelpers.CairoRectangle (gr, rBack, CornerRadius, BorderWidth); } } else { diff --git a/Crow/src/Widgets/PrivateContainer.cs b/Crow/src/Widgets/PrivateContainer.cs index fc5cb348..351ea202 100644 --- a/Crow/src/Widgets/PrivateContainer.cs +++ b/Crow/src/Widgets/PrivateContainer.cs @@ -177,8 +177,6 @@ namespace Crow protected override void UpdateCache (Context ctx) { Rectangle rb = Slot + Parent.ClientRectangle.Position; - - Context gr = new Context (bmp); if (!Clipping.IsEmpty) { diff --git a/Crow/src/Widgets/TabView.cs b/Crow/src/Widgets/TabView.cs index 057afa41..d93014ce 100644 --- a/Crow/src/Widgets/TabView.cs +++ b/Crow/src/Widgets/TabView.cs @@ -258,8 +258,6 @@ namespace Crow if (SelectedTab < tabItms.Length && SelectedTab >= 0) selTabViewIdx = (Children [SelectedTab] as TabItem).ViewIndex; - childrenRWLock.ExitReadLock (); - int i = 0; while (i < selTabViewIdx) { tabItms [i].Paint (ref gr); @@ -273,7 +271,9 @@ namespace Crow if (selTabViewIdx >= 0) tabItms [selTabViewIdx].Paint (ref gr); - + + childrenRWLock.ExitReadLock (); + gr.Restore (); } diff --git a/Samples/BasicTests/BasicTests.cs b/Samples/BasicTests/BasicTests.cs index 54bb8215..cc2285d6 100644 --- a/Samples/BasicTests/BasicTests.cs +++ b/Samples/BasicTests/BasicTests.cs @@ -30,9 +30,9 @@ namespace tests //testFiles = new string [] { @"Interfaces/Experimental/testDock.crow" }; //testFiles = new string [] { @"Interfaces/Divers/welcome.crow" }; - testFiles = new string [] { @"Interfaces/TemplatedGroup/3.crow" }; + //testFiles = new string [] { @"Interfaces/TemplatedGroup/3.crow" }; //testFiles = new string [] { @"Interfaces/Divers/perfMeasures.crow" }; - //testFiles = new string [] { @"Interfaces/Divers/colorPicker.crow" }; + testFiles = new string [] { @"Interfaces/Divers/colorPicker2.crow" }; testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/GraphicObject", "*.crow")).ToArray (); testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Container", "*.crow")).ToArray (); testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Group", "*.crow")).ToArray (); diff --git a/Samples/HelloWorld/HelloWorld.csproj b/Samples/HelloWorld/HelloWorld.csproj index 908af2fc..7d3574d0 100644 --- a/Samples/HelloWorld/HelloWorld.csproj +++ b/Samples/HelloWorld/HelloWorld.csproj @@ -23,7 +23,31 @@ HelloWorld.%(Filename)%(Extension) - - + + + + + + + + + + + + + + + compile + + + compile + + + compile + + + compile + + diff --git a/Samples/HelloWorld/ILView.cs b/Samples/HelloWorld/ILView.cs index 9bac2299..cb974801 100644 --- a/Samples/HelloWorld/ILView.cs +++ b/Samples/HelloWorld/ILView.cs @@ -1,15 +1,35 @@ -// Copyright (c) 2020 Jean-Philippe Bruyère -// -// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +/* Copyright (c) 2020 Jean-Philippe Bruyère + * + * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + */ + using System; using Crow; +namespace test { + public class TestClass { + int a = 0; + } +} + namespace HelloWorld { public class ILView : Widget { + readonly int a; + #region CTOR public ILView () { + a = 10; } + #endregion + void test () + { +#if DEBUG +Console.WriteLine ("debug" + a.ToString()); + +#endif + } + } } diff --git a/Samples/HelloWorld/main.cs b/Samples/HelloWorld/main.cs index c407b04e..75a2683b 100644 --- a/Samples/HelloWorld/main.cs +++ b/Samples/HelloWorld/main.cs @@ -1,66 +1,296 @@ using System; +using System.CodeDom.Compiler; +using System.Collections.Generic; +using System.Composition; +using System.IO; using System.Linq; using System.Reflection; using System.Reflection.Emit; +using System.Threading.Tasks; using Crow; using Crow.IML; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Completion; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Emit; +using Microsoft.CodeAnalysis.Host.Mef; +using Microsoft.CodeAnalysis.MSBuild; +using Microsoft.CodeAnalysis.Text; -namespace HelloWorld +/*namespace RoslynVarRewrite { - class Program : Interface { - Command CMDQuit; - static void Main (string[] args) { - using (Program vke = new Program ()) { - vke.Run (); - } + public class VarRewriter : CSharpSyntaxRewriter + { + private readonly SemanticModel model; + + public VarRewriter (SemanticModel model) + { + this.model = model; } - - protected override void Startup () + + public override SyntaxNode VisitLocalDeclarationStatement (LocalDeclarationStatementSyntax node) { - CMDQuit = new Command (new Action (() => running = false)) { Caption = "Quit", Icon = new SvgPicture ("#Crow.Icons.exit-symbol.svg") }; + var symbolInfo = model.GetSymbolInfo (node.Declaration.Type); + var typeSymbol = symbolInfo.Symbol; + var type = typeSymbol.ToDisplayString ( + SymbolDisplayFormat.MinimallyQualifiedFormat); - Widget w = Load ("#HelloWorld.helloworld.crow"); - w.KeyPress += W_KeyPress; - w.DataSource = this; + var declaration = SyntaxFactory + .LocalDeclarationStatement ( + SyntaxFactory + .VariableDeclaration (SyntaxFactory.IdentifierName ( + SyntaxFactory.Identifier (type))) + .WithVariables (node.Declaration.Variables) + .NormalizeWhitespace () + ) + .WithTriviaFrom (node); + return declaration; } + } - public SolidColor testColor = Color.Red; - public SolidColor TestColor { - get => testColor; - set { - if (testColor == value) - return; - testColor = value; - NotifyValueChanged ("TestColor", testColor); - } + class Program + { + static void Main (string [] args) + { + var tree = CSharpSyntaxTree.ParseText (@" +using System; +using System.Collections.Generic; +class Program { + static void Main(string[] args) { + var x = 5; + var s = ""Test string""; + var l = new List(); + var scores = new byte[8][]; // Test comment + var names = new string[3] {""Diego"", ""Dani"", ""Seba""}; + } +}"); + + // Get the assembly file, the compilation and the semantic model + var Mscorlib = MetadataReference.CreateFromFile (typeof (object).Assembly.Location); + var compilation = CSharpCompilation.Create ("RoslynVarRewrite", + syntaxTrees: new [] { tree }, + references: new [] { Mscorlib }); + var model = compilation.GetSemanticModel (tree); + + var varRewriter = new VarRewriter (model); + var result = varRewriter.Visit (tree.GetRoot ()); + Console.WriteLine (result.ToFullString ()); } + } +}*/ +namespace HelloWorld +{ + class Program : Interface + { + //static void Main () + //{ + // using (Stream s = new FileStream("test.txt", FileMode.Create)) { + // using (StreamWriter sw = new StreamWriter (s)) { + // foreach (SyntaxKind v in Enum.GetValues(typeof(SyntaxKind))) { + // //sw.WriteLine ($"{v,-50} = {(int)v:X8}"); + // //sw.WriteLine ($"{v,-50} = {Convert.ToString ((int)v, 2).PadLeft (16, '0')}"); + // sw.WriteLine ($"case SyntaxKind.{v}:"); + // sw.WriteLine ($"\ttf = editor.formatting [\"default\"];"); + // sw.WriteLine ($"\tbreak;"); + // } + // } + // } + //} + // public class CustomWalker : CSharpSyntaxWalker + // { + // static int Tabs = 0; + // bool cancel; + // int visibleLines, firstLine, currentLine, printedLines; + + // public CustomWalker (int firstLine = 0, int visibleLines = 1) : base (SyntaxWalkerDepth.StructuredTrivia) + // { + // this.visibleLines = visibleLines; + // this.firstLine = firstLine; + // currentLine = 0; + // printedLines = (firstLine == 0) ? 0 : - 1;//<0 until firstLine is reached + + // } + // public override void DefaultVisit (SyntaxNode node) + // { + // if (!cancel) + // base.DefaultVisit (node); + // } + // public override void Visit (SyntaxNode node) + // { + // if (cancel) + // return; - Color [] colors = { Color.Blue, Color.DarkGoldenRod, Color.Red, Color.Azure, Color.Brown, Color.Black, Color.White, Color.Pink }; - int ptr = 0; + // FileLinePositionSpan ls = node.SyntaxTree.GetLineSpan(node.FullSpan); + // currentLine = ls.StartLinePosition.Line; + // if (ls.EndLinePosition.Line >= firstLine) + // base.Visit (node); + // } + // public override void VisitToken (SyntaxToken token) + // { + // if (cancel) + // return; - void W_KeyPress (object sender, KeyPressEventArgs e) + // if ((int)Depth >= 2) { + // Console.ForegroundColor = ConsoleColor.Blue; + // VisitLeadingTrivia (token); + // if (cancel) + // return; + // if (printedLines >= 0) { + // Console.ForegroundColor = ConsoleColor.White; + // Console.Write ($"{(token.RawKind)}{token.ToString ()}"); + // } + // Console.ForegroundColor = ConsoleColor.Green; + // VisitTrailingTrivia (token); + // } + // } + + // bool print => printedLines >= 0 && printedLines < visibleLines; + + // //public override void VisitLeadingTrivia (SyntaxToken token) + // //{ + // // leadingTrivia = true; + // // base.VisitLeadingTrivia (token); + // //} + // //public override void VisitTrailingTrivia (SyntaxToken token) + // //{ + // // if (cancel) + // // return; + // // leadingTrivia = false; + // // base.VisitTrailingTrivia (token); + // //} + // //bool leadingTrivia; + // public override void VisitTrivia (SyntaxTrivia trivia) + // { + + // if (cancel) + // return; + + // base.VisitTrivia (trivia); + + // if (print) { + // if (trivia.IsKind (SyntaxKind.EndOfLineTrivia)) { + // Console.Write ($"(eof)"); + // printedLines++; + // cancel = printedLines == visibleLines; + // } + // Console.Write ($"{trivia.ToString ()}"); + // } + + // if (trivia.IsKind (SyntaxKind.EndOfLineTrivia)) { + // currentLine++; + // if (printedLines < 0) { + // if (currentLine == firstLine) + // printedLines = 0; + // } + // } + + // } + // } + + // static void Main (string [] args) + // { + // var tree = CSharpSyntaxTree.ParseText (@"// Copyright (c) 2013-2019 Bruyère Jean-Philippe + //// + //// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + + //using System; + //using System.IO; + + //public class MyClass + //{ + // //this is a leading trivia + // public void MyMethod() + // { + // int a = 10; + //#if DEBUG + // int b = a + 10; + //#endif + // } + ///* this is a block comment + //*/ + // public void MyMethod(int n) + // { + // } + //}"); + + // var walker = new CustomWalker (4,5); + // walker.Visit (tree.GetRoot ()); + //} + + //Command CMDQuit; + static void Main (string [] args) { - switch (e.KeyChar) { - case 'w': - LoadIMLFragment ($""); - break; - case 'x': - LoadIMLFragment ($""); - break; + Environment.SetEnvironmentVariable ("MSBUILD_EXE_PATH", Path.Combine (msbuildRoot, "MSBuild.dll")); + Environment.SetEnvironmentVariable ("MSBUILD_NUGET_PATH", "/home/jp/.nuget/packages"); + Environment.SetEnvironmentVariable ("COREHOST_TRACE", "1"); + + AppDomain currentDomain = AppDomain.CurrentDomain; + currentDomain.AssemblyResolve += msbuildAssembliesResolve; + + using (Program vke = new Program ()) { + vke.Run (); } + } + + //const string msbuildRoot = "/usr/lib/mono/msbuild/Current/bin/"; + const string msbuildRoot = "/usr/share/dotnet/sdk/3.1.101/"; + const string toolsVersion = "Current"; - ptr++; - if (ptr == colors.Length) - ptr = 0; + static Assembly msbuildAssembliesResolve (object sender, ResolveEventArgs args) + { + string assemblyPath = Path.Combine (msbuildRoot, new AssemblyName (args.Name).Name + ".dll"); + /*Console.BackgroundColor = ConsoleColor.DarkBlue; + Console.ForegroundColor = ConsoleColor.White; + Console.WriteLine ($"Probing: {assemblyPath}"); + Console.ResetColor ();*/ + if (!File.Exists (assemblyPath)) return null; + Assembly assembly = Assembly.LoadFrom (assemblyPath); + return assembly; } + + //protected override void Startup () + //{ + // CMDQuit = new Command (new Action (() => running = false)) { Caption = "Quit", Icon = new SvgPicture ("#Crow.Icons.exit-symbol.svg") }; + + // Widget w = Load ("#HelloWorld.helloworld.crow"); + // w.DataSource = this; + //} + + //public string source = @" + // using System; + + // namespace RoslynCompileSample + // { + // public class Writer + // { + // public void Write(string message) + // { + // Console.WriteLine(message); + // } + // } + // }"; + //public string Source { + // get => source; + // set { + // if (source == value) + // return; + // source = value; + // NotifyValueChanged ("Source", source); + // } + //} + + + public override bool OnKeyDown (Key key) { switch (key) { - case Key.d: - dump (); - break; - case Key.space: - TestColor = Color.Green; + //case Key.F2: + //compile (); + //break; + case Key.F3: + Task t = new Task (testWS); + t.Start (); break; default: return base.OnKeyDown (key); @@ -68,33 +298,135 @@ namespace HelloWorld return true; } - void dump () + //void printChild (IndentedTextWriter stream, CSharpSyntaxNode n) + //{ + // stream.WriteLine ($"{n.Kind()} {n.ToString()}"); + // stream.Indent++; + // foreach (CSharpSyntaxNode item in n.ChildNodes()) { + // printChild (stream, item); + // } + // stream.Indent--; + //} + string slnPath = "/mnt/devel/glfw-sharp/glfw-sharp.sln"; + async void testWS () { - Instantiator inst = Instantiators ["#HelloWorld.helloworld.crow"]; + if (!File.Exists (slnPath)) { + Console.WriteLine ($"File not found: {slnPath}"); + return; + } + Dictionary globalProperties = new Dictionary (); - foreach (Delegate cd in inst.CachedDelegates) { - FieldInfo mb = typeof(Delegate).GetField("original_method_info", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField); + Console.WriteLine ("starting Compilation."); - DynamicMethod dynMethod = mb.GetValue (cd) as DynamicMethod; - /*Console.WriteLine (typeof (ILGenerator).GetFields (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField)); + //globalProperties ["NuGetPackageRoot"] = "/home/jp/.nuget/packages"; + //globalProperties ["RoslynTargetsPath"] = Path.Combine (msbuildRoot, "Roslyn/"); + //globalProperties ["MSBuildSDKsPath"] = Path.Combine (msbuildRoot, "Sdks/"); + //globalProperties ["AdditionalLibPaths"] = msbuildRoot; + //globalProperties ["AssemblySearchPaths"] = msbuildRoot; + foreach (var item in MSBuildMefHostServices.DefaultAssemblies) { + Console.WriteLine ($"{item.FullName}"); + } + + var host = MefHostServices.Create (MSBuildMefHostServices.DefaultAssemblies); - var ilgen = dynMethod.GetILGenerator (); - + using (var workspace = MSBuildWorkspace.Create (globalProperties)) { + //workspace. Properties["BuildingInsideVisualStudio"] = "false"; + workspace.WorkspaceFailed += (sender, e) => Console.WriteLine ($"Workspace error: {e.Diagnostic}"); + Console.WriteLine ($"Opening Solution {slnPath}"); + var solution = await workspace.OpenSolutionAsync (slnPath, new ProgressLog ()); + Console.WriteLine ($"Proj Count:{solution.Projects.Count ()}"); + foreach (Project project in solution.Projects) { + Console.WriteLine ($"Compiling project:{project.FilePath}"); + Compilation compilation = await project.GetCompilationAsync (); + Document doc = project.Documents.First (); -*/ + CompletionService.GetService (doc); + using (var ms = new MemoryStream ()) { + EmitResult result = compilation.Emit (ms); - MethodBody body = dynMethod.GetMethodBody (); + if (!result.Success) { + IEnumerable failures = result.Diagnostics.Where (diagnostic => + diagnostic.IsWarningAsError || + diagnostic.Severity == DiagnosticSeverity.Error); + + foreach (Diagnostic diagnostic in failures) { + Console.Error.WriteLine ("{0}: {1}", diagnostic.Id, diagnostic.GetMessage ()); + } + } else { + Console.WriteLine ("Compilation ok."); + } + } - byte [] il = body.GetILAsByteArray (); - foreach (byte b in il) { - Console.Write ($"{b:x2} "); - //Console.WriteLine (b); } + //workspace.TryApplyChanges() } - + Console.WriteLine ($"end."); + } + } + class ProgressLog : IProgress + { + public void Report (ProjectLoadProgress value) + { + Console.WriteLine ($"{value.ElapsedTime} {value.Operation} {value.TargetFramework}"); } - } + } +//void compile () +//{ +// string assemblyName = Path.GetRandomFileName (); +// MetadataReference [] references = new MetadataReference [] +// { +// MetadataReference.CreateFromFile(typeof(object).Assembly.Location), +// MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location) +// }; +// SourceText txt = SourceText.From (source); + +// SyntaxTree syntaxTree = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText (txt); +// //CSharpCompilationOptions options = new CSharpCompilationOptions (); +// CSharpCompilation compilation = CSharpCompilation.Create( +// assemblyName, +// syntaxTrees: new [] { syntaxTree }, +// references: references, +// options: new CSharpCompilationOptions (OutputKind.DynamicallyLinkedLibrary)); + + +// CSharpSyntaxNode n = (CSharpSyntaxNode)syntaxTree.GetRoot (); + +// using (StringWriter sw = new StringWriter ()) { +// using (IndentedTextWriter itw = new IndentedTextWriter (sw)) +// printChild (itw, n); + +// Console.Write (sw); +// } + +// using (var ms = new MemoryStream ()) { +// EmitResult result = compilation.Emit (ms); + +// if (!result.Success) { +// IEnumerable failures = result.Diagnostics.Where (diagnostic => +// diagnostic.IsWarningAsError || +// diagnostic.Severity == DiagnosticSeverity.Error); + +// foreach (Diagnostic diagnostic in failures) { +// Console.Error.WriteLine ("{0}: {1}", diagnostic.Id, diagnostic.GetMessage ()); +// } +// } else { +// ms.Seek (0, SeekOrigin.Begin); +// Assembly assembly = Assembly.Load (ms.ToArray ()); +// Type type = assembly.GetType ("RoslynCompileSample.Writer"); +// object obj = Activator.CreateInstance (type); +// type.InvokeMember ("Write", BindingFlags.Default | BindingFlags.InvokeMethod, +// null, +// obj, +// new object [] { "Hello World" }); +// } +// } + + + +//} +// } +//} diff --git a/Samples/HelloWorld/ui/helloworld.crow b/Samples/HelloWorld/ui/helloworld.crow index 1bc9601f..f18e4bd8 100644 --- a/Samples/HelloWorld/ui/helloworld.crow +++ b/Samples/HelloWorld/ui/helloworld.crow @@ -1,6 +1,5 @@  - - + \ No newline at end of file