[svn] r7618: vs-plugin/trunk: Nemerle.Compiler.Utils/FormCodeDomGenerator.n Nemerle.Compiler.Utils/Nemerle...

VladD2 svnadmin at nemerle.org
Tue Apr 24 16:02:33 CEST 2007


Log:
Work on CodeDomParser (WinForms designer).
At now WinForms designer work. But exists problem with save docs and code generation is not good.

Author: VladD2
Date: Tue Apr 24 16:02:30 2007
New Revision: 7618

Added:
   vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/TextManagement/IFileTextMerger.n
Modified:
   vs-plugin/trunk/Nemerle.Compiler.Utils/FormCodeDomGenerator.n
   vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Compiler.Utils.csproj
   vs-plugin/trunk/Nemerle.VsIntegration/Project/RDTFileTextMerger.cs
   vs-plugin/trunk/Nemerle.VsIntegration/Utils.cs

Modified: vs-plugin/trunk/Nemerle.Compiler.Utils/FormCodeDomGenerator.n
==============================================================================
--- vs-plugin/trunk/Nemerle.Compiler.Utils/FormCodeDomGenerator.n	(original)
+++ vs-plugin/trunk/Nemerle.Compiler.Utils/FormCodeDomGenerator.n	Tue Apr 24 16:02:30 2007
@@ -20,15 +20,6 @@
 
 namespace Nemerle.Compiler.Utils
 {
-  public interface IFileTextMerger
-  {
-    AddLines(start: int, newLines: IList[string]) : void;
-    ReplaceLines(start: int, end: int, newLines: IList[string]) : void;
-    RemoveLines(start: int, end: int) : void;
-
-    Flush() : void;
-  }
-
   // now FormCodeDomGenerator only parses files from Project.CompileUnits
   // it is not thread-safe at the moment!
 
@@ -96,16 +87,16 @@
 
       Options = if (o != null) o else CodeGeneratorOptions();
 
-      assert(e.Namespaces.Count == 1,"CodeCompileUnit for a form should contain only one namespace!");
+      Debug.Assert(e.Namespaces.Count == 1,"CodeCompileUnit for a form should contain only one namespace!");
 
       // TODO : do we need to bother about namespace Imports?
 
-      assert(e.Namespaces[0].Types.Count == 1,
+      Debug.Assert(e.Namespaces[0].Types.Count == 1,
              "CodeCompileUnit for a form should contain only one form class!");
 
       def nsDecls = project.CompileUnits[_mainFileIndex].Decls.MapFilterByType.[Decl.Namespace]();
 
-      assert(nsDecls.Length == 1,
+      Debug.Assert(nsDecls.Length == 1,
              "Root Decl for a form CompileUnit should contain only one form class!" );
       def nsDecl = nsDecls.Head;
 
@@ -113,7 +104,8 @@
       MergeClassDecl(e.Namespaces[0].Types[0],
                      match(nsDecl.Decls.Find(_ is Decl.Type ))
                      { | Some(typeBuilder) => (typeBuilder :> Decl.Type)
-                       | _ => throw ApplicationException("Can't find typeBuilder for the Form class in CodeDom")
+                       | _ => throw ApplicationException(
+                                "Can't find typeBuilder for the Form class in CodeDom")
                      }.Builder);
 
       _mainFileMerger.Flush();

Modified: vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Compiler.Utils.csproj
==============================================================================
--- vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Compiler.Utils.csproj	(original)
+++ vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Compiler.Utils.csproj	Tue Apr 24 16:02:30 2007
@@ -169,6 +169,9 @@
   <ItemGroup>
     <Compile Include="Nemerle.Completion2\CodeFormatting\TokenStreamFinder.n" />
   </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Nemerle.Completion2\TextManagement\IFileTextMerger.n" />
+  </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

Added: vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/TextManagement/IFileTextMerger.n
==============================================================================
--- (empty file)
+++ vs-plugin/trunk/Nemerle.Compiler.Utils/Nemerle.Completion2/TextManagement/IFileTextMerger.n	Tue Apr 24 16:02:30 2007
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Nemerle.Compiler.Utils
+{
+  public interface IFileTextMerger
+  {
+    AddLines(start: int, newLines: IList[string]) : void;
+    ReplaceLines(start: int, end: int, newLines: IList[string]) : void;
+    RemoveLines(start: int, end: int) : void;
+
+    Flush() : void;
+  }
+}

Modified: vs-plugin/trunk/Nemerle.VsIntegration/Project/RDTFileTextMerger.cs
==============================================================================
--- vs-plugin/trunk/Nemerle.VsIntegration/Project/RDTFileTextMerger.cs	(original)
+++ vs-plugin/trunk/Nemerle.VsIntegration/Project/RDTFileTextMerger.cs	Tue Apr 24 16:02:30 2007
@@ -22,7 +22,11 @@
 	{
 		#region FileChange variant
 
-		class FileChange{}
+		abstract class FileChange
+		{
+			public abstract void ProcessChanges(IVsTextLines vsTextLines, TextSpan[] span);
+		}
+		
 		class AddLinesChange : FileChange 
 		{
 			public readonly int Start;
@@ -33,7 +37,34 @@
 				Start = start;
 				NewLines = new List<string>(newLines);
 			}
+
+			public override void ProcessChanges(IVsTextLines vsTextLines, TextSpan[] span)
+			{
+				string text = Utils.ConvertToText(this.NewLines);
+
+				Debug.Print("AddLinesChange : add to line " + this.Start +
+					" lines (" + this.NewLines.Count + ") :\n"
+					+ text);
+
+				/*int endLine = this.Start + this.NewLines.Count;
+				int endLength;
+				ErrorHandler.ThrowOnFailure(vsTextLines.GetLengthOfLine(endLine, out endLength));
+				*/
+				GCHandle handle = GCHandle.Alloc(text, GCHandleType.Pinned);
+				try
+				{
+					ErrorHandler.ThrowOnFailure(
+						vsTextLines.ReplaceLines(this.Start - 1, 0,
+												 this.Start - 1, 0,
+												 handle.AddrOfPinnedObject(), text.Length, span));
+				}
+				finally
+				{
+					handle.Free();
+				}
 		}
+		}
+
 		class ReplaceLinesChange : FileChange
 		{
 			public readonly int Start;
@@ -46,7 +77,32 @@
 				End = end;
 				NewLines = new List<string>(newLines);
 			}
+
+			public override void ProcessChanges(IVsTextLines vsTextLines, TextSpan[] span)
+			{
+				string text = Utils.ConvertToText(this.NewLines);
+
+				Debug.Print("RemoveLinesChange : remove lines (" + this.Start +
+							"," + this.End + ")");
+
+				//int endLength;
+				//ErrorHandler.ThrowOnFailure(vsTextLines.GetLengthOfLine(this.End-1, out endLength));
+
+				GCHandle handle = GCHandle.Alloc(text, GCHandleType.Pinned);
+				try
+				{
+					ErrorHandler.ThrowOnFailure(
+						vsTextLines.ReplaceLines(this.Start - 1, 0,
+												 this.End, 0, // endLength,
+												 handle.AddrOfPinnedObject(), text.Length, span));
+				}
+				finally
+				{
+					handle.Free();
+				}
+			}
 		}
+
 		class RemoveLinesChange : FileChange
 		{
 			public readonly int Start;
@@ -57,6 +113,30 @@
 				Start = start;
 				End = end;
 			}
+
+			public override void ProcessChanges(IVsTextLines vsTextLines, TextSpan[] span)
+			{
+				string text = "";
+
+				Debug.Print("RemoveLinesChange : replace lines (" + this.Start +
+								"," + this.End + ") with\n" + text);
+
+				//int endLength;
+				//ErrorHandler.ThrowOnFailure(vsTextLines.GetLengthOfLine(this.End-1, out endLength));
+
+				GCHandle handle = GCHandle.Alloc(text, GCHandleType.Pinned);
+				try
+				{
+					ErrorHandler.ThrowOnFailure(
+						vsTextLines.ReplaceLines(this.Start - 1, 0,
+												 this.End, 0, //endLength,
+												 handle.AddrOfPinnedObject(), text.Length, span));
+				}
+				finally
+				{
+					handle.Free();
+				}
+			}
 		}
 
 		#endregion
@@ -82,127 +162,34 @@
 
 		public void AddLines(int start, IList<string> newLines)
 		{
-			lock( _fileChanges ) // TODO : maybe more fine grained operation?
-			{
 				_fileChanges.Add(new AddLinesChange(start, newLines));
 			}
-		}
 
 		public void ReplaceLines(int start, int end, IList<string> newLines)
 		{
-			lock( _fileChanges ) // TODO : maybe more fine grained operation?
-			{
 				_fileChanges.Add(new ReplaceLinesChange(start, end, newLines));
 			}
-		}
 
 		public void RemoveLines(int start, int end)
 		{
-			lock( _fileChanges ) // TODO : maybe more fine grained operation?
-			{
 				_fileChanges.Add(new RemoveLinesChange(start, end));
 			}
-		}
 
 		private delegate void ProcessFileChangesFunc(IVsTextLines vsTextLines);
 
 		private void ProcessFileChanges(IVsTextLines vsTextLines)
 		{
 			TextSpan[] span = new TextSpan[1];
-			lock( _fileChanges ) // TODO : maybe more fine grained operation?
-			{
-				foreach (FileChange fileChange in _fileChanges)
-				{
-					{
-						AddLinesChange addLinesChange = fileChange as AddLinesChange;
-						if (addLinesChange != null)
-						{
-							Debug.Print("AddLinesChange : add to line " + addLinesChange.Start +
-										" lines ("+ addLinesChange.NewLines.Count + ") :\n" + GetText(addLinesChange.NewLines));
-
-							string text = GetText(addLinesChange.NewLines);
-							/*int endLine = addLinesChange.Start + addLinesChange.NewLines.Count;
-							int endLength;
-							ErrorHandler.ThrowOnFailure(vsTextLines.GetLengthOfLine(endLine, out endLength));
-							*/
-							GCHandle handle = GCHandle.Alloc(text, GCHandleType.Pinned);
-							try
-							{
-								ErrorHandler.ThrowOnFailure(
-									vsTextLines.ReplaceLines(addLinesChange.Start-1, 0,
-															 addLinesChange.Start-1, 0,
-															 handle.AddrOfPinnedObject(), text.Length, span));
-							}
-							finally
-							{
-								handle.Free();
-							}
-							continue;
-						}
-					}
-
-					{
-						ReplaceLinesChange replaceLinesChange = fileChange as ReplaceLinesChange;
-						if (replaceLinesChange != null)
-						{
-							Debug.Print("ReplaceLinesChange : replace lines (" + replaceLinesChange.Start +
-										"," + replaceLinesChange.End + ") with\n" + GetText(replaceLinesChange.NewLines));
-
-							string text = GetText(replaceLinesChange.NewLines);
-							//int endLength;
-							//ErrorHandler.ThrowOnFailure(vsTextLines.GetLengthOfLine(replaceLinesChange.End-1, out endLength));
-
-							GCHandle handle = GCHandle.Alloc(text, GCHandleType.Pinned);
-							try
-							{
-								ErrorHandler.ThrowOnFailure(
-									vsTextLines.ReplaceLines(replaceLinesChange.Start-1, 0,
-															 replaceLinesChange.End, 0, // endLength,
-															 handle.AddrOfPinnedObject(), text.Length, span));
-							}
-							finally
-							{
-								handle.Free();
-							}
-							continue;
-						}
-					}
 
-					{
-						RemoveLinesChange removeLinesChange = fileChange as RemoveLinesChange;
-						if (removeLinesChange != null)
-						{
-							Debug.Print("RemoveLinesChange : remove lines ("+ removeLinesChange.Start +
-										","+ removeLinesChange.End + ")");
-
-							string text = "";
-							//int endLength;
-							//ErrorHandler.ThrowOnFailure(vsTextLines.GetLengthOfLine(removeLinesChange.End-1, out endLength));
-
-							GCHandle handle = GCHandle.Alloc(text, GCHandleType.Pinned);
-							try
-							{
-								ErrorHandler.ThrowOnFailure(
-									vsTextLines.ReplaceLines(removeLinesChange.Start-1, 0,
-															 removeLinesChange.End, 0, //endLength,
-															 handle.AddrOfPinnedObject(), text.Length, span));
-							}
-							finally
-							{
-								handle.Free();
-							}
-							continue;
-						}
-					}
-				}
+			foreach (FileChange fileChange in _fileChanges)
+				fileChange.ProcessChanges(vsTextLines, span);
 
 				_fileChanges.Clear();
 			}
-		}
 
 		public void Flush()
 		{
-			ProcessFileChangesHelper( ProcessFileChanges );
+			ProcessFileChangesHelper(ProcessFileChanges);
 			// TODO - how to handle a case when file is not in RDT?
 		}
 
@@ -214,18 +201,34 @@
 
 			// (kberes) Shouldn't this be an InvalidOperationException instead with some not to annoying errormessage to the user?
 			if (rdt == null)
-			{
-				ErrorHandler.ThrowOnFailure(VSConstants.E_FAIL);
-			}
+				ErrorHandler.ThrowOnFailure(VSConstants.E_FAIL); // Fixme: 
 
 			IVsHierarchy hier;
-			uint itemid, cookie;
+			uint itemId, cookie;
 			IntPtr docData = IntPtr.Zero;
-			//Getting a edit lock on the document. Must be released later.
-			ErrorHandler.ThrowOnFailure(rdt.FindAndLockDocument((uint)(_VSRDTFLAGS.RDT_EditLock), filePath, out hier, out itemid, out docData, out cookie));
+
+			// Find the document in rdt.
+			// Getting a edit lock on the document. Must be released later.
+			ErrorHandler.ThrowOnFailure(rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_EditLock, 
+				filePath, out hier, out itemId, out docData, out cookie));
+
+			if (docData == IntPtr.Zero) // if file not open (usually it's a *.Designer.n)...
+			{ // ... open it.
+				FileDocumentManager manager = (FileDocumentManager)_fileNode.GetDocumentManager();
+				IVsWindowFrame frame;
+
+				// Open the document in rdt.
+				ErrorHandler.ThrowOnFailure(manager.Open(false, false, NativeMethods.LOGVIEWID_Code,
+					out frame, WindowFrameShowAction.DontShow));
+				// Find the document in rdt.
+				ErrorHandler.ThrowOnFailure(rdt.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_EditLock,
+					filePath, out hier, out itemId, out docData, out cookie));
+			}
+
 			if (docData != IntPtr.Zero)
 			{
-				IVsPersistDocData persistDocData = Marshal.GetObjectForIUnknown(docData) as IVsPersistDocData;
+				object obj = Marshal.GetObjectForIUnknown(docData);
+				IVsPersistDocData persistDocData = obj as IVsPersistDocData;
 				Marshal.Release(docData);
 
 				try
@@ -237,9 +240,7 @@
 						// Try getting a text buffer provider first
 						IVsTextBufferProvider srpTextBufferProvider = persistDocData as IVsTextBufferProvider;
 						if (srpTextBufferProvider != null)
-						{
 							ErrorHandler.ThrowOnFailure(srpTextBufferProvider.GetTextBuffer(out srpTextLines));
-						}
 						// TODO : handle null case
 					}
 
@@ -250,7 +251,7 @@
 					ErrorHandler.ThrowOnFailure(srpTextLines.LockBuffer());
 					try
 					{
-						processFunc( srpTextLines );
+						processFunc(srpTextLines);
 					}
 					finally
 					{
@@ -269,16 +270,5 @@
 				throw new ApplicationException("Document hasn't been found in RDT");
 			}
 		}
-
-		// helper function
-		private string GetText(List<string> lines)
-		{
-			StringBuilder builder = new StringBuilder();
-
-			foreach (string s in lines)
-				builder.AppendLine(s);
-
-			return builder.ToString();
-		}
 	}
 }

Modified: vs-plugin/trunk/Nemerle.VsIntegration/Utils.cs
==============================================================================
--- vs-plugin/trunk/Nemerle.VsIntegration/Utils.cs	(original)
+++ vs-plugin/trunk/Nemerle.VsIntegration/Utils.cs	Tue Apr 24 16:02:30 2007
@@ -1,20 +1,25 @@
 using System;
 using System.IO;
+using System.Collections.Generic;
 
 using Microsoft.VisualStudio.Package;
-
 using Nemerle.Compiler;
-
 using Nemerle.VisualStudio.LanguageService;
-
 using Msbuild = Microsoft.Build.BuildEngine;
-
 using Microsoft.VisualStudio.TextManager.Interop;
 
 namespace Nemerle.VisualStudio
 {
 	static class Utils
 	{
+		public static string ConvertToText(List<string> lines)
+		{
+			string[] ary = new string[lines.Count + 1];
+			lines.CopyTo(ary);
+			ary[ary.Length - 1] = System.Environment.NewLine;
+			return string.Join(System.Environment.NewLine, ary);
+		}
+
 		public static Location LocationFromSpan(int fileIndex, TextSpan span)
 		{
 			return new Location(fileIndex, span.iStartLine + 1, span.iStartIndex + 1, span.iEndLine + 1, span.iEndIndex + 1);



More information about the svn mailing list