[svn] r6392: nemerle/trunk/ncc: completion/CodeCompletionEngine.n testsuite/completion-playground/compl-s2...

VladD2 svnadmin at nemerle.org
Mon Jun 12 16:35:49 CEST 2006


Log:
Add possibility complete inside expression. Modify compl3.n to use this feature and colorize output. 
To nazgul & malekith: Look on the "//FixMe:" section in ncc\testsuite\completion-playground\compl3.n

Author: VladD2
Date: Mon Jun 12 16:35:47 2006
New Revision: 6392

Modified:
   nemerle/trunk/ncc/completion/CodeCompletionEngine.n
   nemerle/trunk/ncc/testsuite/completion-playground/compl-s2.n
   nemerle/trunk/ncc/testsuite/completion-playground/compl3.n

Modified: nemerle/trunk/ncc/completion/CodeCompletionEngine.n
==============================================================================
--- nemerle/trunk/ncc/completion/CodeCompletionEngine.n	(original)
+++ nemerle/trunk/ncc/completion/CodeCompletionEngine.n	Mon Jun 12 16:35:47 2006
@@ -33,7 +33,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
  
+#define DEBUG
+ 
 using System;
+using Nemerle.Assertions;
 using Nemerle.Compiler;
 using Nemerle.Utility;
 using Nemerle.Collections;
@@ -42,8 +45,6 @@
 using Typed = Nemerle.Compiler.Typedtree;
 using SR = System.Reflection;
 
-#define DEBUG
-
 namespace Nemerle.Completion
 {
     public delegate CompletionStageHandler (node : TypeInfo) : void;
@@ -359,6 +360,39 @@
             completionList
         }
         
+        public RunCompletionEngine (
+          [NotNull] observedMethod : MethodBuilder, 
+          [NotNull] contents       : string,
+          completionPosition       : int) 
+          : CompletionResult
+            requires completionPosition <= contents.Length
+        {
+            // Tell the methods we are in completion mode
+            is_completion = true;
+
+            mutable completionList = null;
+            def env = observedMethod.DeclaringType.GlobalEnv;
+
+            def contents = if (completionPosition == contents.Length)
+              contents + " " else contents;
+
+            def lexer = LexerCompletion (this, contents, completionPosition);
+            observedMethod.GetHeader().body =
+              FunBody.Parsed (MainParser.ParseExpr (env, lexer, true));
+        
+            try
+            {
+              observedMethod.RunBodyTyper ();
+            }
+            catch
+            {
+            | e is CompletionResult => completionList = e;
+            | e => System.Console.WriteLine (e.Message);
+            }
+            
+            completionList
+        }
+
         mutable listMessages : list[CompilerMessage];
         process_error_message (location : Location, message : string) : void
         {

Modified: nemerle/trunk/ncc/testsuite/completion-playground/compl-s2.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/completion-playground/compl-s2.n	(original)
+++ nemerle/trunk/ncc/testsuite/completion-playground/compl-s2.n	Mon Jun 12 16:35:47 2006
@@ -6,7 +6,7 @@
 #define foobar
 
 namespace Example {
-  public class SomeClass {
+  public class SomeClass[T,X] {
   
     mutable n : string;
   

Modified: nemerle/trunk/ncc/testsuite/completion-playground/compl3.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/completion-playground/compl3.n	(original)
+++ nemerle/trunk/ncc/testsuite/completion-playground/compl3.n	Mon Jun 12 16:35:47 2006
@@ -2,6 +2,7 @@
 using Nemerle.Completion;
 using Nemerle.Collections;
 using Nemerle.Utility;
+using System;
 using System.IO;
 using System.Console;
 
@@ -31,7 +32,7 @@
 // mutable the_method = null;
 
 mutable count = 0;
-mutable completionMember : IMethod;
+mutable completionMember : MethodBuilder;
 
 def loop (x)
 {
@@ -60,7 +61,7 @@
                 match (n.GetKind ())
                 { | Method (method) => // IMethod
                       when (method.Name == "Bar")
-                        completionMember = method;
+                      completionMember = method :> MethodBuilder;
                   | _    => ()
                 }
               }
@@ -80,45 +81,144 @@
 
 def try_completion (body)
 {
+  def PrintLine(color = ConsoleColor.White)
+  {
+    ForegroundColor = color;
+    WriteLine ("=================================================");
+    ResetColor ();
+  }
+
+  // Execute function f and colorize it console output.
+  def ColorizeOut[T](color, f : void -> T) : T
+  {
+    ForegroundColor = color;
+    mutable result;
+    try { result = f() } finally { ResetColor(); }
+    result
+  }
+
   try
   {
-    WriteLine ($"\nTrying to complete: '$body'");
-    def completionResult = engine.RunCompletionEngine (completionMember, body);
+    PrintLine(ConsoleColor.Cyan);
+
+    // If success return body without completion sybmbol and completion 
+    // position. Otherwise return empty strung and -1 in completionPosition.
+    def ExtractCompletionPosition(body)
+    {
+      def completionPosition = body.IndexOf('?');
+      if (completionPosition < 0)
+        ("", -1)
+      else
+      {
+        def part1 = body.Substring(0, completionPosition);
+        def part2 = body.Substring(completionPosition + 1);
+        def complBody = part1 + part2;
+
+        // Print method body with highlight completion position.
+        Write("'" + part1);
+        ForegroundColor = ConsoleColor.Yellow;
+        Write ("?");
+        ResetColor ();
+        WriteLine (part2 + "'");
+
+        (complBody, completionPosition)
+      }
+    }
+
+    def (complBody, complPosition) = ExtractCompletionPosition(body);
+
+    if (complPosition < 0)
+      ColorizeOut(ConsoleColor.Red, () => WriteLine(
+        $"No completion position! Please, specify it by '?' symbol: '$body'"));
+    else
+    {
+      def completionResult = ColorizeOut(ConsoleColor.Yellow, () => 
+        engine.RunCompletionEngine(completionMember, complBody, complPosition));
 
     if (completionResult == null)
       WriteLine ("  No member found!!!");
     else
+        ColorizeOut(ConsoleColor.Magenta, () => 
     {
-      WriteLine ("  Type of the member: " + Engine.GetNameFromType (completionResult.ObjectType));
+          WriteLine ("  Type of the member: " 
+            + Engine.GetNameFromType (completionResult.ObjectType));
       WriteLine ("  Member found:");
 
+          //System.Type SQ_ExtensionAttribute = Assembly.GetType ("System.Runtime.CompilerServices.ExtensionAttribute");
+          def extensionAttribute = typeof(Nemerle.Internal.ExtensionAttribute);
+
       foreach (overl in completionResult.Overloads)
-        WriteLine ($"    $(overl.Member.Name)  ==> $overl"
-          " $(overl.Member.Attributes)");
+          {
+            def isExtension = overl.Member.GetHandle().IsDefined (
+              extensionAttribute, false);
 
-      WriteLine ("  ==========");
+            def typ = if (isExtension)
+              match ((overl.Member :> IMethod).GetParameters ())
+              {
+                | extParam :: _ => extParam.ty
+                | _ => throw Exception();
+              }
+            else
+              null;
       
-      WriteLine ("Finished");
-      WriteLine ("================================================");
+            WriteLine ($"    $(overl.Member.Name)  ==> $overl"
+              " $(overl.Member.Attributes) Is extension method $isExtension $typ");
+          }
+        });
     }
   }
-  catch { ex => WriteLine(ex); }
+  catch { | ex => ColorizeOut(ConsoleColor.Red, () => WriteLine(ex)) }
+
+  PrintLine(ConsoleColor.Cyan);
+  ColorizeOut(ConsoleColor.Cyan, () => WriteLine("Finished"));
+  PrintLine(ConsoleColor.Cyan);
 }
 
 //FixMe: Bugs
-try_completion ("");
-try_completion ("def _ = 0; ");
-try_completion ("def arr = array[1, 2, 3]; arr.M");
+try_completion ("Fo?");
+try_completion ("this.Fo?");
+try_completion ("def arr = array[1, 2, 3]; arr.M?");
+try_completion (@"
+                def arr = array['1', '2'];
+                arr.Fold('0', (_, _) => '3').Map?");
+
+
+//TODO: Imlement this.
+try_completion (" ? ");
+try_completion ("def _ = 0; ? ");
+
 
 // Work fine:
-try_completion ("NArray.M");
-try_completion ("def a = \"a\"; a.");
-try_completion ("int.");
-//try_completion ("Fo");
-//try_completion ("System.");
-//try_completion ("System.Reflection.");
-//try_completion ("this.Fo");
-//try_completion ("string.For");
-//try_completion ("string.");
-//try_completion ("Q");
-//try_completion ("def s = System.Collections.Generic.List (); s.");
+try_completion (@"
+           def x = Dictionary();
+           def y = x.Count;
+           do
+           {
+             ihg kjhg khg @#|45z6;
+             x[""a""] = 1;
+             //x[""a""];
+             x[""a""].ToString().Su?
+             //kljshdflkjh lkdhjfsk;
+             //this.
+           }
+           while (false);
+           ");
+
+try_completion (@"
+  def x = ['1', '2', '3'];
+  match (x)
+  {
+    | [a] => a
+    | [_, b, _] => b
+    | lst => lst.?
+  }
+");
+try_completion ("NArray.M?");
+try_completion ("def a = \"a\"; a.?");
+try_completion ("int.?");
+try_completion ("System.?");
+try_completion ("System.Reflection.?");
+try_completion ("string.For?");
+try_completion ("string.?");
+try_completion ("Q?");
+try_completion ("def s = System.Collections.Generic.List (); s.?");



More information about the svn mailing list