public override Task <TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            SourceText text;

            if (IdeApp.Workbench?.Documents.Any(doc => doc.IsFile && doc.FileName != null && FilePath.PathComparer.Compare(Path.GetFullPath(doc.FileName), fileName) == 0) == true)
            {
                text = new MonoDevelopSourceText(TextFileProvider.Instance.GetTextEditorData(fileName).CreateDocumentSnapshot());
            }
            else
            {
                try {
                    if (File.Exists(fileName))
                    {
                        text = SourceText.From(TextFileUtility.GetText(fileName));
                    }
                    else
                    {
                        text = SourceText.From("");
                    }
                } catch (Exception e) {
                    LoggingService.LogError($"Failed to get file text for {fileName}", e);
                    text = SourceText.From("");
                }
            }
            return(Task.FromResult(TextAndVersion.Create(text, VersionStamp.Create())));
        }
Esempio n. 2
0
        async Task <TextAndVersion> GetTextAndVersion(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (!File.Exists(fileName))
            {
                var document = ((MonoDevelopWorkspace)workspace).GetDocument(documentId);
                if (document == null)
                {
                    return(null);
                }
                return(TextAndVersion.Create(await document.GetTextAsync(cancellationToken), VersionStamp.Create()));
            }
            SourceText text;

            if (workspace.IsDocumentOpen(documentId))
            {
                text = new MonoDevelopSourceText(TextFileProvider.Instance.GetTextEditorData(fileName).CreateDocumentSnapshot());
            }
            else
            {
                text = SourceText.From(await TextFileUtility.GetTextAsync(fileName, cancellationToken));
            }
            return(TextAndVersion.Create(text, VersionStamp.Create()));
        }
        public override Task <TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            SourceText text;

            if (IdeServices.DocumentManager?.Documents.Any(doc => doc.IsFile && doc.FileName != null && FilePath.PathComparer.Compare(Path.GetFullPath(doc.FileName), fileName) == 0 && doc.Editor != null) == true)
            {
                var document = IdeServices.DocumentManager?.Documents.FirstOrDefault(doc => doc.IsFile && doc.FileName != null && FilePath.PathComparer.Compare(Path.GetFullPath(doc.FileName), fileName) == 0 && doc.Editor != null);
                text = MonoDevelopSourceText.Create(document.Editor);
            }
            else
            {
                try {
                    if (File.Exists(fileName))
                    {
                        text = SourceText.From(TextFileUtility.GetText(fileName));
                    }
                    else
                    {
                        text = SourceText.From("");
                    }
                } catch (Exception e) {
                    LoggingService.LogError($"Failed to get file text for {fileName}", e);
                    text = SourceText.From("");
                }
            }
            return(Task.FromResult(TextAndVersion.Create(text, VersionStamp.Create())));
        }
		public void Dispose ()
		{
			if (isDisposed)
				return;
			currentText = null;
			document.TextChanging -= HandleTextReplacing;
			document.TextChanged -= Document_TextChanged;;
			isDisposed = true;
		}
		TextAndVersion GetTextAndVersion (Workspace workspace, DocumentId documentId)
		{
			SourceText text;
			if (workspace.IsDocumentOpen (documentId)) {
				text = new MonoDevelopSourceText (TextFileProvider.Instance.GetTextEditorData (fileName).CreateDocumentSnapshot ());
			}
			else {
				text = SourceText.From (MonoDevelop.Core.Text.TextFileUtility.GetText (fileName));
			}
			return TextAndVersion.Create (text, VersionStamp.Create ());
		}
Esempio n. 6
0
 public void Dispose()
 {
     if (isDisposed)
     {
         return;
     }
     currentText            = null;
     document.TextChanging -= HandleTextReplacing;
     document.TextChanged  -= Document_TextChanged;;
     isDisposed             = true;
 }
		async Task<TextAndVersion> GetTextAndVersion (Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
		{
			cancellationToken.ThrowIfCancellationRequested ();
			SourceText text;
			if (IdeApp.Workbench?.Documents.Any (doc => FilePath.PathComparer.Compare (doc.FileName, fileName) == 0) == true) {
				text = new MonoDevelopSourceText (TextFileProvider.Instance.GetTextEditorData (fileName).CreateDocumentSnapshot ());
			} else {
				if (!File.Exists (fileName))
					return TextAndVersion.Create (SourceText.From (""), VersionStamp.Create ()); ;
				text = SourceText.From (await TextFileUtility.GetTextAsync (fileName, cancellationToken));
			}
			return TextAndVersion.Create (text, VersionStamp.Create ());
		}
		async Task<TextAndVersion> GetTextAndVersion (Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
		{
			if (!File.Exists (fileName)) {
				return TextAndVersion.Create (await ((MonoDevelopWorkspace)workspace).GetDocument (documentId).GetTextAsync (cancellationToken), VersionStamp.Create ());
			}
			SourceText text;
			if (workspace.IsDocumentOpen (documentId)) {
				text = new MonoDevelopSourceText (TextFileProvider.Instance.GetTextEditorData (fileName).CreateDocumentSnapshot ());
			}
			else {
				text = SourceText.From (TextFileUtility.GetText (fileName));
			}
			return TextAndVersion.Create (text, VersionStamp.Create ());
		}
Esempio n. 9
0
        TextAndVersion GetTextAndVersion(Workspace workspace, DocumentId documentId)
        {
            SourceText text;

            if (workspace.IsDocumentOpen(documentId))
            {
                text = new MonoDevelopSourceText(TextFileProvider.Instance.GetTextEditorData(fileName).CreateDocumentSnapshot());
            }
            else
            {
                text = SourceText.From(MonoDevelop.Core.Text.TextFileUtility.GetText(fileName));
            }
            return(TextAndVersion.Create(text, VersionStamp.Create()));
        }
Esempio n. 10
0
        public override async Task <TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            SourceText text;

            if (IdeApp.Workbench?.Documents.Any(doc => FilePath.PathComparer.Compare(doc.FileName, fileName) == 0) == true)
            {
                text = new MonoDevelopSourceText(TextFileProvider.Instance.GetTextEditorData(fileName).CreateDocumentSnapshot());
            }
            else
            {
                if (!File.Exists(fileName))
                {
                    return(TextAndVersion.Create(SourceText.From(""), VersionStamp.Create()));
                }
                text = SourceText.From(await TextFileUtility.GetTextAsync(fileName, cancellationToken).ConfigureAwait(false));
            }
            return(TextAndVersion.Create(text, VersionStamp.Create()));
        }
		void Document_TextChanged (object sender, Core.Text.TextChangeEventArgs e)
		{
			currentText = null;
		}
Esempio n. 12
0
 public TextLineCollectionWrapper(MonoDevelopSourceText parent, IReadonlyTextDocument textDoc)
 {
     this.parent  = parent;
     this.textDoc = textDoc;
 }
Esempio n. 13
0
        protected override void ApplyDocumentTextChanged(DocumentId id, SourceText text)
        {
            var document = GetDocument(id);

            if (document == null)
            {
                return;
            }
            bool isOpen;
            var  filePath = document.FilePath;
            var  data     = TextFileProvider.Instance.GetTextEditorData(filePath, out isOpen);

            // Guard against already done changes in linked files.
            // This shouldn't happen but the roslyn merging seems not to be working correctly in all cases :/
            if (document.GetLinkedDocumentIds().Length > 0 && isOpen && !(text.GetType().FullName == "Microsoft.CodeAnalysis.Text.ChangedText"))
            {
                return;
            }
            SourceText formerText;

            if (changedFiles.TryGetValue(filePath, out formerText))
            {
                if (formerText.Length == text.Length && formerText.ToString() == text.ToString())
                {
                    return;
                }
            }
            changedFiles [filePath] = text;

            Projection projection = null;

            foreach (var entry in ProjectionList)
            {
                var p = entry.Projections.FirstOrDefault(proj => FilePath.PathComparer.Equals(proj.Document.FileName, filePath));
                if (p != null)
                {
                    filePath   = entry.File.FilePath;
                    projection = p;
                    break;
                }
            }
            SourceText oldFile;

            if (!isOpen || !document.TryGetText(out oldFile))
            {
                oldFile = new MonoDevelopSourceText(data);
            }
            var changes = text.GetTextChanges(oldFile).OrderByDescending(c => c.Span.Start).ToList();
            int delta   = 0;

            if (!isOpen)
            {
                delta = ApplyChanges(projection, data, changes);
                var    formatter   = CodeFormatterService.GetFormatter(data.MimeType);
                var    mp          = GetMonoProject(CurrentSolution.GetProject(id.ProjectId));
                string currentText = data.Text;

                foreach (var change in changes)
                {
                    delta -= change.Span.Length - change.NewText.Length;
                    var startOffset = change.Span.Start - delta;

                    if (projection != null)
                    {
                        int originalOffset;
                        if (projection.TryConvertFromProjectionToOriginal(startOffset, out originalOffset))
                        {
                            startOffset = originalOffset;
                        }
                    }

                    string str;
                    if (change.NewText.Length == 0)
                    {
                        str = formatter.FormatText(mp.Policies, currentText, TextSegment.FromBounds(Math.Max(0, startOffset - 1), Math.Min(data.Length, startOffset + 1)));
                    }
                    else
                    {
                        str = formatter.FormatText(mp.Policies, currentText, new TextSegment(startOffset, change.NewText.Length));
                    }
                    data.ReplaceText(startOffset, change.NewText.Length, str);
                }
                data.Save();
                OnDocumentTextChanged(id, new MonoDevelopSourceText(data), PreservationMode.PreserveValue);
                FileService.NotifyFileChanged(filePath);
            }
            else
            {
                var formatter       = CodeFormatterService.GetFormatter(data.MimeType);
                var documentContext = IdeApp.Workbench.Documents.FirstOrDefault(d => FilePath.PathComparer.Compare(d.FileName, filePath) == 0);
                if (documentContext != null)
                {
                    var editor = (TextEditor)data;
                    using (var undo = editor.OpenUndoGroup()) {
                        delta = ApplyChanges(projection, data, changes);

                        foreach (var change in changes)
                        {
                            delta -= change.Span.Length - change.NewText.Length;
                            var startOffset = change.Span.Start - delta;
                            if (projection != null)
                            {
                                int originalOffset;
                                if (projection.TryConvertFromProjectionToOriginal(startOffset, out originalOffset))
                                {
                                    startOffset = originalOffset;
                                }
                            }
                            if (change.NewText.Length == 0)
                            {
                                formatter.OnTheFlyFormat(editor, documentContext, TextSegment.FromBounds(Math.Max(0, startOffset - 1), Math.Min(data.Length, startOffset + 1)));
                            }
                            else
                            {
                                formatter.OnTheFlyFormat(editor, documentContext, new TextSegment(startOffset, change.NewText.Length));
                            }
                        }
                    }
                }
                OnDocumentTextChanged(id, new MonoDevelopSourceText(data.CreateDocumentSnapshot()), PreservationMode.PreserveValue);
                Runtime.RunInMainThread(() => {
                    if (IdeApp.Workbench != null)
                    {
                        foreach (var w in IdeApp.Workbench.Documents)
                        {
                            w.StartReparseThread();
                        }
                    }
                });
            }
        }
		protected override void ApplyDocumentTextChanged (DocumentId id, SourceText text)
		{
			var document = GetDocument (id);
			if (document == null)
				return;
			bool isOpen;
			var filePath = document.FilePath;
			var data = TextFileProvider.Instance.GetTextEditorData (filePath, out isOpen);

			// Guard against already done changes in linked files.
			// This shouldn't happen but the roslyn merging seems not to be working correctly in all cases :/
			if (document.GetLinkedDocumentIds ().Length > 0 && isOpen && !(text.GetType ().FullName == "Microsoft.CodeAnalysis.Text.ChangedText")) {
				return;
			}
			SourceText formerText;
			if (changedFiles.TryGetValue (filePath, out formerText)) {
				if (formerText.Length == text.Length && formerText.ToString () == text.ToString ())
					return;
			}
			changedFiles [filePath] = text;
		
			Projection projection = null;
			foreach (var entry in ProjectionList) {
				var p = entry.Projections.FirstOrDefault (proj => FilePath.PathComparer.Equals (proj.Document.FileName, filePath));
				if (p != null) {
					filePath = entry.File.FilePath;
					projection = p;
					break;
				}
			}
			SourceText oldFile;
			if (!isOpen || !document.TryGetText (out oldFile)) {
				oldFile = new MonoDevelopSourceText (data);
			}
			var changes = text.GetTextChanges (oldFile).OrderByDescending (c => c.Span.Start).ToList ();
			int delta = 0;

			if (!isOpen) {
				delta = ApplyChanges (projection, data, changes);
				var formatter = CodeFormatterService.GetFormatter (data.MimeType);
				var mp = GetMonoProject (CurrentSolution.GetProject (id.ProjectId));
				string currentText = data.Text;

				foreach (var change in changes) {
					delta -= change.Span.Length - change.NewText.Length;
					var startOffset = change.Span.Start - delta;

					if (projection != null) {
						int originalOffset;
						if (projection.TryConvertFromProjectionToOriginal (startOffset, out originalOffset))
							startOffset = originalOffset;
					}

					string str;
					if (change.NewText.Length == 0) {
						str = formatter.FormatText (mp.Policies, currentText, TextSegment.FromBounds (Math.Max (0, startOffset - 1), Math.Min (data.Length, startOffset + 1)));
					} else {
						str = formatter.FormatText (mp.Policies, currentText, new TextSegment (startOffset, change.NewText.Length));
					}
					data.ReplaceText (startOffset, change.NewText.Length, str);
				}
				data.Save ();
				OnDocumentTextChanged (id, new MonoDevelopSourceText (data), PreservationMode.PreserveValue);
				FileService.NotifyFileChanged (filePath);
			} else {
				var formatter = CodeFormatterService.GetFormatter (data.MimeType); 
				var documentContext = IdeApp.Workbench.Documents.FirstOrDefault (d => FilePath.PathComparer.Compare (d.FileName, filePath) == 0);
				if (documentContext != null) {
					var editor = (TextEditor)data;
					using (var undo = editor.OpenUndoGroup ()) {
						delta = ApplyChanges (projection, data, changes);

						foreach (var change in changes) {
							delta -= change.Span.Length - change.NewText.Length;
							var startOffset = change.Span.Start - delta;
							if (projection != null) {
								int originalOffset;
								if (projection.TryConvertFromProjectionToOriginal (startOffset, out originalOffset))
									startOffset = originalOffset;
							}
							if (change.NewText.Length == 0) {
								formatter.OnTheFlyFormat (editor, documentContext, TextSegment.FromBounds (Math.Max (0, startOffset - 1), Math.Min (data.Length, startOffset + 1)));
							} else {
								formatter.OnTheFlyFormat (editor, documentContext, new TextSegment (startOffset, change.NewText.Length));
							}
						}
					}
				}
				OnDocumentTextChanged (id, new MonoDevelopSourceText(data.CreateDocumentSnapshot ()), PreservationMode.PreserveValue);
				Runtime.RunInMainThread (() => {
					if (IdeApp.Workbench != null)
						foreach (var w in IdeApp.Workbench.Documents)
							w.StartReparseThread ();
				});
			}
		}
			public TextLineCollectionWrapper (MonoDevelopSourceText parent, IReadonlyTextDocument textDoc)
			{
				this.parent = parent;
				this.textDoc = textDoc;
			}
 void Document_TextChanged(object sender, Core.Text.TextChangeEventArgs e)
 {
     currentText = null;
 }