internal static CacheIndentEngine CreateEngine(string text, out SourceText sourceText, OptionSet options = null)
		{
			if (options == null) {
				options = FormattingOptionsFactory.CreateMono ();
				//	options.AlignToFirstIndexerArgument = formatOptions.AlignToFirstMethodCallArgument = true;
			}

			var sb = new StringBuilder ();
			int offset = 0;
			for (int i = 0; i < text.Length; i++) {
				var ch = text [i];
				if (ch == '$') {
					offset = i;
					continue;
				}
				sb.Append (ch);
			}


			sourceText = SourceText.From (sb.ToString ());

			var result = new CacheIndentEngine (new CSharpIndentEngine (options));
			result.Update (sourceText, offset);
			return result;
		}
		public static IDocumentIndentEngine CreateEngine (string text)
		{
			var sb = new StringBuilder ();
			int offset = 0;
			for (int i = 0; i < text.Length; i++) {
				var ch = text [i];
				if (ch == '$') {
					offset = i;
					continue;
				}
				sb.Append (ch);
			}

			var tww = new TestWorkbenchWindow ();
			var content = new TestViewContent ();
			tww.ViewContent = content;
			content.ContentName = "/a.json";
			content.Data.MimeType = "application/json";

			content.Data.Text = sb.ToString ();
			var doc = new MonoDevelop.Ide.Gui.Document (tww);

			var csi = new JSonIndentEngine (content.Data, doc);
			var result = new CacheIndentEngine (csi);
			result.Update (content.Data, offset);
			return result;
		}
		protected override void Initialize ()
		{
			base.Initialize ();
			IStateMachineIndentEngine indentEngine;
			indentEngine = new JSonIndentEngine (Editor, DocumentContext);
			stateTracker = new CacheIndentEngine (indentEngine);
			Editor.SetIndentationTracker (new JSonIndentationTracker (Editor, stateTracker));
		}
예제 #4
0
        async void HandleTextOptionsChanged(object sender, EventArgs e)
        {
            //var options = Editor.CreateNRefactoryTextEditorOptions ();
            var optionTask = DocumentContext?.AnalysisDocument?.GetOptionsAsync();

            if (optionTask == null)
            {
                return;
            }
            optionSet = await optionTask;
            if (optionSet == null)
            {
                return;
            }
            //options.IndentBlankLines = true;
            ICSharpCode.NRefactory6.CSharp.IStateMachineIndentEngine indentEngine;
            try {
                var csharpIndentEngine = new ICSharpCode.NRefactory6.CSharp.CSharpIndentEngine(optionSet);
                //csharpIndentEngine.EnableCustomIndentLevels = true;
                foreach (var symbol in GetDefinedSymbols(DocumentContext.Project))
                {
                    csharpIndentEngine.DefineSymbol(symbol);
                }
                indentEngine = csharpIndentEngine;
            } catch (Exception ex) {
                LoggingService.LogError("Error while creating the c# indentation engine", ex);
                indentEngine = new ICSharpCode.NRefactory6.CSharp.NullIStateMachineIndentEngine();
            }

            await Runtime.RunInMainThread(delegate {
                stateTracker = new ICSharpCode.NRefactory6.CSharp.CacheIndentEngine(indentEngine);
                if (DefaultSourceEditorOptions.Instance.IndentStyle == IndentStyle.Auto)
                {
                    Editor.IndentationTracker = null;
                }
                else
                {
                    Editor.IndentationTracker = new IndentVirtualSpaceManager(Editor, stateTracker);
                }

                indentationDisabled = DefaultSourceEditorOptions.Instance.IndentStyle == IndentStyle.Auto || DefaultSourceEditorOptions.Instance.IndentStyle == IndentStyle.None;
                if (indentationDisabled)
                {
                    Editor.SetTextPasteHandler(null);
                }
                else
                {
                    Editor.SetTextPasteHandler(new CSharpTextPasteHandler(this, stateTracker, optionSet));
                }
            });
        }
 public override void Dispose()
 {
     if (Editor != null)
     {
         Editor.SetTextPasteHandler(null);
         Editor.OptionsChanged -= HandleTextOptionsChanged;
         Editor.SetIndentationTracker(null);
         Editor.TextChanging -= HandleTextReplacing;
         Editor.TextChanged  -= HandleTextReplaced;
     }
     IdeApp.Workspace.ActiveConfigurationChanged -= HandleTextOptionsChanged;
     stateTracker = null;
     base.Dispose();
 }
        async void HandleTextOptionsChanged(object sender, EventArgs e)
        {
            optionSet = await DocumentContext.GetOptionsAsync();

            IStateMachineIndentEngine indentEngine;

            try {
                var csharpIndentEngine = new ICSharpCode.NRefactory6.CSharp.CSharpIndentEngine(optionSet);
                //csharpIndentEngine.EnableCustomIndentLevels = true;
                foreach (var symbol in GetDefinedSymbols(DocumentContext.Project))
                {
                    csharpIndentEngine.DefineSymbol(symbol);
                }
                indentEngine = csharpIndentEngine;
            } catch (Exception ex) {
                LoggingService.LogError("Error while creating the c# indentation engine", ex);
                indentEngine = new ICSharpCode.NRefactory6.CSharp.NullIStateMachineIndentEngine();
            }

            await Runtime.RunInMainThread(delegate {
                try {
                    var editor = Editor;
                    if (editor == null)                     // disposed
                    {
                        return;
                    }
                    stateTracker = new ICSharpCode.NRefactory6.CSharp.CacheIndentEngine(indentEngine);
                    if (DefaultSourceEditorOptions.Instance.IndentStyle == IndentStyle.Auto)
                    {
                        editor.IndentationTracker = null;
                    }
                    else
                    {
                        editor.IndentationTracker = new CSharpIndentationTracker(editor, DocumentContext);
                    }

                    indentationDisabled = DefaultSourceEditorOptions.Instance.IndentStyle == IndentStyle.Auto || DefaultSourceEditorOptions.Instance.IndentStyle == IndentStyle.None;
                    if (indentationDisabled)
                    {
                        editor.SetTextPasteHandler(null);
                    }
                    else
                    {
                        editor.SetTextPasteHandler(new CSharpTextPasteHandler(this, optionSet));
                    }
                } catch (Exception ex) {
                    LoggingService.LogError("Error while handling text option change.", ex);
                }
            });
        }
예제 #7
0
        public override void Dispose()
        {
            if (Editor != null)
            {
                Editor.SetTextPasteHandler(null);
                Editor.OptionsChanged    -= HandleTextOptionsChanged;
                Editor.IndentationTracker = null;
                Editor.TextChanging      -= HandleTextReplacing;
                Editor.TextChanged       -= HandleTextReplaced;
                DocumentContext.AnalysisDocumentChanged -= HandleTextOptionsChanged;
            }
            IdeApp.Workspace.ActiveConfigurationChanged -= HandleTextOptionsChanged;
            CompletionWindowManager.WindowClosed        -= CompletionWindowManager_WindowClosed;

            stateTracker = null;
            base.Dispose();
        }
예제 #8
0
		public static IDocumentIndentEngine CreateEngine(string text, OptionSet formatOptions = null, IEnumerable<string> symbols = null)
		{
			var policy = formatOptions;
			if (policy == null) {
					policy = FormattingOptionsFactory.CreateMono();

//				policy.IndentPreprocessorDirectives = false;
//				policy.AlignToFirstMethodCallArgument = policy.AlignToFirstIndexerArgument = true;

			}

			var sb = new StringBuilder();
			int offset = 0;
			for (int i = 0; i < text.Length; i++)
			{
				var ch = text[i];
				if (ch == '$')
				{
					offset = i;
					continue;
				}
				sb.Append(ch);
			}

			var document = SourceText.From(sb.ToString());

			var csi = new CSharpIndentEngine(policy) {
				EnableCustomIndentLevels = true
			};
			if (symbols != null) {
				foreach (var sym in symbols) {
					csi.DefineSymbol(sym);
				}
			}
			var result = new CacheIndentEngine(csi);
			result.Update(document, offset);
			return result;
		}
		ITextPasteHandler CreateTextPasteIndentEngine (CacheIndentEngine indent, OptionSet optionSet)
		{
			return new TextPasteIndentEngine (indent, optionSet) {
				InUnitTestMode = true
			};
		}
		public override void Dispose ()
		{
			if (Editor != null) {
				Editor.SetTextPasteHandler (null);
				Editor.OptionsChanged -= HandleTextOptionsChanged;
				Editor.SetIndentationTracker (null);
				Editor.TextChanging -= HandleTextReplacing;
				Editor.TextChanged -= HandleTextReplaced;
			}
			IdeApp.Workspace.ActiveConfigurationChanged -= HandleTextOptionsChanged;
			stateTracker = null;
			base.Dispose ();
		}
		void HandleTextOptionsChanged (object sender, EventArgs e)
		{
			//var options = Editor.CreateNRefactoryTextEditorOptions ();
			optionSet = Policy.CreateOptions (Editor.Options);
			//options.IndentBlankLines = true;
			ICSharpCode.NRefactory6.CSharp.IStateMachineIndentEngine indentEngine;
			try {
				var csharpIndentEngine = new ICSharpCode.NRefactory6.CSharp.CSharpIndentEngine (optionSet);
				//csharpIndentEngine.EnableCustomIndentLevels = true;
				foreach (var symbol in GetDefinedSymbols (DocumentContext.Project)) {
					csharpIndentEngine.DefineSymbol (symbol);
				}
				indentEngine = csharpIndentEngine;
			} catch (Exception ex) {
				LoggingService.LogError ("Error while creating the c# indentation engine", ex);
				indentEngine = new ICSharpCode.NRefactory6.CSharp.NullIStateMachineIndentEngine ();
			}
			stateTracker = new ICSharpCode.NRefactory6.CSharp.CacheIndentEngine (indentEngine);
			if (DefaultSourceEditorOptions.Instance.IndentStyle == IndentStyle.Auto) {
				Editor.SetIndentationTracker (null);
			} else {
				Editor.SetIndentationTracker (new IndentVirtualSpaceManager (Editor, stateTracker));
			}

			indentationDisabled = DefaultSourceEditorOptions.Instance.IndentStyle == IndentStyle.Auto || DefaultSourceEditorOptions.Instance.IndentStyle == IndentStyle.None;
			if (indentationDisabled) {
				Editor.SetTextPasteHandler (null);
			} else {
				Editor.SetTextPasteHandler (new CSharpTextPasteHandler (this, stateTracker, optionSet));
			}
		}
		public JSonIndentationTracker(TextEditor data, CacheIndentEngine stateTracker)
		{
			this.data = data;
			this.stateTracker = stateTracker;
		}
예제 #13
0
		public static void RandomTests(string filePath, int count, OptionSet options = null)
		{
			if (File.Exists(filePath))
			{
				var code = File.ReadAllText(filePath);
				var document = SourceText.From(code);
				options = options ?? FormattingOptionsFactory.CreateMono();

				var engine = new CacheIndentEngine(new CSharpIndentEngine(options) { EnableCustomIndentLevels = true });
				Random rnd = new Random();

				for (int i = 0; i < count; i++) {
					int offset = rnd.Next(document.Length);
					engine.Update(document, offset);
					if (engine.CurrentIndent.Length == 0)
						continue;
				}

			}
			else
			{
				Assert.Fail("File " + filePath + " doesn't exist.");
			}
		}
예제 #14
0
		public static void ReadAndTest(string filePath, OptionSet options = null)
		{
			if (File.Exists(filePath))
			{
				filePath = Path.GetFullPath(filePath);
				var code = File.ReadAllText(filePath);
				var document = SourceText.From(code);
				if (options == null) {
					options = FormattingOptionsFactory.CreateMono();
					//policy.AlignToFirstIndexerArgument = policy.AlignToFirstMethodCallArgument = true;
				}

				var engine = new CacheIndentEngine(new CSharpIndentEngine(options) { EnableCustomIndentLevels = true });
				int errors = 0;

				var newLine = options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);
				foreach (var ch in code)
				{
					if (newLine [0] == ch)
					{
						if (!(engine.LineBeganInsideMultiLineComment || engine.LineBeganInsideVerbatimString)) {
							if (engine.CurrentIndent.Length > 0) {
								if (engine.NeedsReindent) {
									errors++;
									var line = document.Lines.GetLineFromPosition(engine.Offset);
									Console.WriteLine(string.Format("Indent: {2}, Current indent: {3} in {0}:{1}", filePath, line.LineNumber, GetIndentString (engine.ThisLineIndent), GetIndentString (engine.CurrentIndent)));
								}
							}
						}
					}

					engine.Push(ch);
				}
				Assert.AreEqual(0, errors, "file has errors");

			}
			else
			{
				Assert.Fail("File " + filePath + " doesn't exist.");
			}
		}
		/// <summary>
		///     Creates a new CacheIndentEngine instance from the given prototype.
		/// </summary>
		/// <param name="prototype">
		///     A CacheIndentEngine instance.
		/// </param>
		public CacheIndentEngine(CacheIndentEngine prototype)
		{
			this.currentEngine = prototype.currentEngine.Clone();
		}
예제 #16
0
 /// <summary>
 ///     Creates a new CacheIndentEngine instance from the given prototype.
 /// </summary>
 /// <param name="prototype">
 ///     A CacheIndentEngine instance.
 /// </param>
 public CacheIndentEngine(CacheIndentEngine prototype)
 {
     this.currentEngine = prototype.currentEngine.Clone();
 }
		public IndentVirtualSpaceManager(TextEditor data, CacheIndentEngine stateTracker)
		{
			this.data = data;
			this.stateTracker = stateTracker;
		}