Beispiel #1
0
        /// <summary>
        /// Gets the set of <see cref="T:TextChange"/> that describe how the text changed
        /// between this text and an older version. This may be multiple detailed changes
        /// or a single change encompassing the entire text.
        /// </summary>
        public virtual IReadOnlyList <TextChange> GetTextChanges(SourceText oldText)
        {
            int newPosDelta = 0;

            var ranges      = this.GetChangeRanges(oldText).ToList();
            var textChanges = new List <TextChange>(ranges.Count);

            foreach (var range in ranges)
            {
                var newPos = range.Span.Start + newPosDelta;

                // determine where in the newText this text exists
                string newt;
                if (range.NewLength > 0)
                {
                    var span = new TextSpan(newPos, range.NewLength);
                    newt = this.ToString(span);
                }
                else
                {
                    newt = string.Empty;
                }

                textChanges.Add(new TextChange(range.Span, newt));

                newPosDelta += range.NewLength - range.Span.Length;
            }

            return(textChanges.ToImmutableListOrEmpty());
        }
Beispiel #2
0
        private async Task <ProjectId> LoadProjectAsync(string projectFilePath, IProjectFileLoader loader, bool preferMetadata, List <ProjectInfo> loadedProjects, CancellationToken cancellationToken)
        {
            System.Diagnostics.Debug.Assert(projectFilePath != null);
            System.Diagnostics.Debug.Assert(loader != null);

            var projectId = this.GetOrCreateProjectId(projectFilePath);

            var name = Path.GetFileNameWithoutExtension(projectFilePath);

            try
            {
                var projectFile = await loader.LoadProjectFileAsync(projectFilePath, this.properties, cancellationToken).ConfigureAwait(false);

                var projectFileInfo = await projectFile.GetProjectFileInfoAsync(cancellationToken).ConfigureAwait(false);

                VersionStamp version;
                if (!string.IsNullOrEmpty(projectFilePath) && File.Exists(projectFilePath))
                {
                    version = VersionStamp.Create(File.GetLastWriteTimeUtc(projectFilePath));
                }
                else
                {
                    version = VersionStamp.Create();
                }

                // Documents
                var docFileInfos = projectFileInfo.Documents.ToImmutableListOrEmpty();
                CheckDocuments(docFileInfos, projectFilePath, projectId);

                var docs = new List <DocumentInfo>();
                foreach (var docFileInfo in docFileInfos)
                {
                    docs.Add(DocumentInfo.Create(
                                 DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                                 Path.GetFileName(docFileInfo.LogicalPath),
                                 GetDocumentFolders(docFileInfo.LogicalPath),
                                 projectFile.GetSourceCodeKind(docFileInfo.FilePath),
                                 new FileTextLoader(docFileInfo.FilePath),
                                 docFileInfo.FilePath,
                                 docFileInfo.IsGenerated));
                }

                // project references
                var resolvedReferences = await this.ResolveProjectReferencesAsync(
                    projectFilePath, projectFileInfo.ProjectReferences, preferMetadata, loadedProjects, cancellationToken).ConfigureAwait(false);

                // metadata references
                var metadataReferences = projectFileInfo.MetadataReferences
                                         .Select(mi => new MetadataFileReference(mi.Path, mi.Properties, this.GetDocumentationProvider(mi.Path)))
                                         .Concat(resolvedReferences.MetadataReferences);

                var outputFilePath = projectFileInfo.OutputFilePath;
                var assemblyName   = projectFileInfo.AssemblyName;

                // if the project file loader couldn't figure out an assembly name, make one using the project's file path.
                if (string.IsNullOrWhiteSpace(assemblyName))
                {
                    assemblyName = Path.GetFileNameWithoutExtension(projectFilePath);

                    // if this is still unreasonable, use a fixed name.
                    if (string.IsNullOrWhiteSpace(assemblyName))
                    {
                        assemblyName = "assembly";
                    }
                }

                loadedProjects.Add(
                    ProjectInfo.Create(
                        projectId,
                        version,
                        name,
                        assemblyName,
                        loader.Language,
                        projectFilePath,
                        outputFilePath,
                        projectFileInfo.CompilationOptions,
                        projectFileInfo.ParseOptions,
                        docs.ToImmutableListOrEmpty(),
                        resolvedReferences.ProjectReferences.ToImmutableListOrEmpty(),
                        metadataReferences.ToImmutableListOrEmpty(),
                        analyzerReferences: projectFileInfo.AnalyzerReferences,
                        isSubmission: false,
                        hostObjectType: null));

                return(projectId);
            }
            catch (System.IO.IOException exception)
            {
                this.OnWorkspaceFailed(new ProjectDiagnostic(WorkspaceDiagnosticKind.FileAccessFailure, exception.Message, projectId));
                loadedProjects.Add(ProjectInfo.Create(projectId, VersionStamp.Default, name, name, loader.Language, filePath: projectFilePath));
                return(projectId);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets the set of <see cref="T:TextChange"/> that describe how the text changed
        /// between this text and an older version. This may be multiple detailed changes 
        /// or a single change encompassing the entire text.
        /// </summary>
        public virtual IReadOnlyList<TextChange> GetTextChanges(SourceText oldText)
        {
            int newPosDelta = 0;

            var ranges = this.GetChangeRanges(oldText).ToList();
            var textChanges = new List<TextChange>(ranges.Count);

            foreach (var range in ranges)
            {
                var newPos = range.Span.Start + newPosDelta;

                // determine where in the newText this text exists
                string newt;
                if (range.NewLength > 0)
                {
                    var span = new TextSpan(newPos, range.NewLength);
                    newt = this.ToString(span);
                }
                else
                {
                    newt = string.Empty;
                }

                textChanges.Add(new TextChange(range.Span, newt));

                newPosDelta += range.NewLength - range.Span.Length;
            }

            return textChanges.ToImmutableListOrEmpty();
        }
Beispiel #4
0
        private async Task<ProjectId> LoadProjectAsync(string projectFilePath, IProjectFileLoader loader, bool preferMetadata, List<ProjectInfo> loadedProjects, CancellationToken cancellationToken)
        {
            System.Diagnostics.Debug.Assert(projectFilePath != null);
            System.Diagnostics.Debug.Assert(loader != null);

            var projectId = this.GetOrCreateProjectId(projectFilePath);

            var name = Path.GetFileNameWithoutExtension(projectFilePath);

            try
            {
                var projectFile = await loader.LoadProjectFileAsync(projectFilePath, this.properties, cancellationToken).ConfigureAwait(false);
                var projectFileInfo = await projectFile.GetProjectFileInfoAsync(cancellationToken).ConfigureAwait(false);

                VersionStamp version;
                if (!string.IsNullOrEmpty(projectFilePath) && File.Exists(projectFilePath))
                {
                    version = VersionStamp.Create(File.GetLastWriteTimeUtc(projectFilePath));
                }
                else
                {
                    version = VersionStamp.Create();
                }

                // Documents
                var docFileInfos = projectFileInfo.Documents.ToImmutableListOrEmpty();
                CheckDocuments(docFileInfos, projectFilePath, projectId);

                var docs = new List<DocumentInfo>();
                foreach (var docFileInfo in docFileInfos)
                {
                    docs.Add(DocumentInfo.Create(
                        DocumentId.CreateNewId(projectId, debugName: docFileInfo.FilePath),
                        Path.GetFileName(docFileInfo.LogicalPath),
                        GetDocumentFolders(docFileInfo.LogicalPath),
                        projectFile.GetSourceCodeKind(docFileInfo.FilePath),
                        new FileTextLoader(docFileInfo.FilePath),
                        docFileInfo.FilePath,
                        docFileInfo.IsGenerated));
                }

                // project references
                var resolvedReferences = await this.ResolveProjectReferencesAsync(
                    projectFilePath, projectFileInfo.ProjectReferences, preferMetadata, loadedProjects, cancellationToken).ConfigureAwait(false);

                // metadata references
                var metadataReferences = projectFileInfo.MetadataReferences
                    .Select(mi => new MetadataFileReference(mi.Path, mi.Properties, this.GetDocumentationProvider(mi.Path)))
                    .Concat(resolvedReferences.MetadataReferences);

                var outputFilePath = projectFileInfo.OutputFilePath;
                var assemblyName = projectFileInfo.AssemblyName;

                // if the project file loader couldn't figure out an assembly name, make one using the project's file path.
                if (string.IsNullOrWhiteSpace(assemblyName))
                {
                    assemblyName = Path.GetFileNameWithoutExtension(projectFilePath);

                    // if this is still unreasonable, use a fixed name.
                    if (string.IsNullOrWhiteSpace(assemblyName))
                    {
                        assemblyName = "assembly";
                    }
                }

                loadedProjects.Add(
                    ProjectInfo.Create(
                        projectId,
                        version,
                        name,
                        assemblyName,
                        loader.Language,
                        projectFilePath,
                        outputFilePath,
                        projectFileInfo.CompilationOptions,
                        projectFileInfo.ParseOptions,
                        docs.ToImmutableListOrEmpty(),
                        resolvedReferences.ProjectReferences.ToImmutableListOrEmpty(),
                        metadataReferences.ToImmutableListOrEmpty(),
                        analyzerReferences: projectFileInfo.AnalyzerReferences,
                        isSubmission: false,
                        hostObjectType: null));

                return projectId;
            }
            catch (System.IO.IOException exception)
            {
                this.OnWorkspaceFailed(new ProjectDiagnostic(WorkspaceDiagnosticKind.FileAccessFailure, exception.Message, projectId));
                loadedProjects.Add(ProjectInfo.Create(projectId, VersionStamp.Default, name, name, loader.Language, filePath: projectFilePath));
                return projectId;
            }
        }