Represents a decorator of an IStateMachineIndentEngine instance that provides logic for reseting and updating the engine on text changed events.
The decorator is based on periodical caching of the engine's state and delegating all logic behind indentation to the currently active engine.
Inheritance: IStateMachineIndentEngine
		public static CacheIndentEngine CreateEngine(string text, CSharpFormattingOptions formatOptions = null, TextEditorOptions options = null)
		{
			if (formatOptions == null) {
				formatOptions = FormattingOptionsFactory.CreateMono();
				formatOptions.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);
			}
			
			var document = new ReadOnlyDocument(sb.ToString());
			options = options ?? new TextEditorOptions { EolMarker = "\n" };
			
			var result = new CacheIndentEngine(new CSharpIndentEngine(document, options, formatOptions));
			result.Update(offset);
			return result;
		}
		public override void Initialize ()
		{
			base.Initialize ();
			IStateMachineIndentEngine indentEngine;
			indentEngine = new JSonIndentEngine (document.Editor);
			stateTracker = new CacheIndentEngine (indentEngine);
			document.Editor.IndentationTracker = new JSonIndentationTracker (document.Editor, stateTracker);
		}
		static void IndentSingleLine(CacheIndentEngine engine, IDocument document, IDocumentLine line)
		{
			engine.Update(line.EndOffset);
			if (engine.NeedsReindent) {
				var indentation = TextUtilities.GetWhitespaceAfter(document, line.Offset);
				// replacing the indentation in two steps is necessary to make the caret move accordingly.
				document.Replace(indentation.Offset, indentation.Length, "");
				document.Replace(indentation.Offset, 0, engine.ThisLineIndent);
				engine.ResetEngineToPosition(line.Offset);
			}
		}
        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 data = new TextEditorData ();
            data.Text = sb.ToString ();
            var csi = new JSonIndentEngine (data);
            var result = new CacheIndentEngine (csi);
            result.Update (offset);
            return result;
        }
Beispiel #5
0
		public static IDocumentIndentEngine CreateEngine(string text, CSharpFormattingOptions 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 = new ReadOnlyDocument(sb.ToString());
			var options = new TextEditorOptions();

			var csi = new CSharpIndentEngine(document, options, policy) {
				EnableCustomIndentLevels = true
			};
			if (symbols != null) {
				foreach (var sym in symbols) {
					csi.DefineSymbol(sym);
				}
			}
			var result = new CacheIndentEngine(csi);
			result.Update(offset);
			return result;
		}
		public override void Dispose ()
		{
			if (textEditorData != null) {
				textEditorData.TextPasteHandler = null;
				textEditorData.Paste -= HandleTextPaste;
				textEditorData.Options.Changed -= HandleTextOptionsChanged;
				textEditorData.IndentationTracker = null;
				textEditorData.Document.TextReplacing -= HandleTextReplacing;
				textEditorData.Document.TextReplaced -= HandleTextReplaced;
			}
			IdeApp.Workspace.ActiveConfigurationChanged -= HandleTextOptionsChanged;
			stateTracker = null;
			base.Dispose ();
		}
		void HandleTextOptionsChanged (object sender, EventArgs e)
		{
			var policy = Policy.CreateOptions ();
			var options = Editor.CreateNRefactoryTextEditorOptions ();
			options.IndentBlankLines = true;
			IStateMachineIndentEngine indentEngine;
			try {
				var csharpIndentEngine = new CSharpIndentEngine (textEditorData.Document, options, policy);
				//csharpIndentEngine.EnableCustomIndentLevels = true;
				foreach (var symbol in MonoDevelop.CSharp.Highlighting.CSharpSyntaxMode.GetDefinedSymbols (document.Project)) {
					csharpIndentEngine.DefineSymbol (symbol);
				}
				indentEngine = csharpIndentEngine;
			} catch (Exception ex) {
				LoggingService.LogError ("Error while creating the c# indentation engine", ex);
				indentEngine = new NullIStateMachineIndentEngine (textEditorData.Document);
			}
			stateTracker = new CacheIndentEngine (indentEngine);
			textEditorData.IndentationTracker = new IndentVirtualSpaceManager (textEditorData, stateTracker);


			if (textEditorData.Options.IndentStyle == IndentStyle.Auto || textEditorData.Options.IndentStyle == IndentStyle.None) {
				textEditorData.TextPasteHandler = null;
			} else {
				textEditorData.TextPasteHandler = new TextPasteIndentEngine (stateTracker, options, policy);
			}
		}
Beispiel #8
0
		public static void RandomTests(string filePath, int count, CSharpFormattingOptions policy = null, TextEditorOptions options = null)
		{
			if (File.Exists(filePath))
			{
				var code = File.ReadAllText(filePath);
				var document = new ReadOnlyDocument(code);
				policy = policy ?? FormattingOptionsFactory.CreateMono();
				options = options ?? new TextEditorOptions { IndentBlankLines = false };

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

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

			}
			else
			{
				Assert.Fail("File " + filePath + " doesn't exist.");
			}
		}
Beispiel #9
0
		public static void ReadAndTest(string filePath, CSharpFormattingOptions policy = null, TextEditorOptions options = null)
		{
			if (File.Exists(filePath))
			{
				filePath = Path.GetFullPath(filePath);
				var code = File.ReadAllText(filePath);
				var document = new ReadOnlyDocument(code);
				if (policy == null) {
					policy = FormattingOptionsFactory.CreateMono();
					policy.AlignToFirstIndexerArgument = policy.AlignToFirstMethodCallArgument = true;
				}
				options = options ?? new TextEditorOptions { IndentBlankLines = false };

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

				foreach (var ch in code)
				{
					if (options.EolMarker[0] == ch)
					{
						if (!(engine.LineBeganInsideMultiLineComment || engine.LineBeganInsideVerbatimString)) {
							if (engine.CurrentIndent.Length > 0) {
								if (engine.NeedsReindent) {
									errors++;
									Console.WriteLine(string.Format("Indent: {2}, Current indent: {3} in {0}:{1}", filePath, engine.Location.Line, engine.ThisLineIndent.Length, engine.CurrentIndent.Length));
								}
							}
						}
					}

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

			}
			else
			{
				Assert.Fail("File " + filePath + " doesn't exist.");
			}
		}
Beispiel #10
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();
 }
 IStateMachineIndentEngine CreateTracker(TextEditorData data)
 {
     var policy = PolicyService.InvariantPolicies.Get <CSharpFormattingPolicy> ("text/x-csharp").CreateOptions();
     var textStylePolicy = data.CreateNRefactoryTextEditorOptions();
     textStylePolicy.IndentBlankLines = true;
     var result = new CacheIndentEngine(new ICSharpCode.NRefactory.CSharp.CSharpIndentEngine(data.Document, textStylePolicy, policy));
     result.Update (data.Caret.Offset);
     return result;
 }
Beispiel #12
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 JSonIndentationTracker(TextEditorData data, CacheIndentEngine stateTracker)
		{
			this.data = data;
			this.stateTracker = stateTracker;
		}
		public IndentVirtualSpaceManager(TextEditorData data, CacheIndentEngine stateTracker)
		{
			this.data = data;
			this.stateTracker = stateTracker;
		}