private void Test(string markup, bool isMissing, bool isLine, ParseOptions options = null)
        {
            int? position;
            TextSpan? expectedSpan;
            string source;
            MarkupTestFile.GetPositionAndSpan(markup, out source, out position, out expectedSpan);
            var tree = SyntaxFactory.ParseSyntaxTree(source, options);

            TextSpan breakpointSpan;
            bool hasBreakpoint = BreakpointSpans.TryGetBreakpointSpan(tree, position.Value, CancellationToken.None, out breakpointSpan);

            if (isLine)
            {
                Assert.True(hasBreakpoint);
                Assert.True(breakpointSpan.Length == 0);
            }
            else if (isMissing)
            {
                Assert.False(hasBreakpoint);
            }
            else
            {
                Assert.True(hasBreakpoint);
                Assert.Equal(expectedSpan.Value, breakpointSpan);
            }
        }
        public static void VerifyAnalyzer(string path, DiagnosticAnalyzer diagnosticAnalyzer, ParseOptions options = null,
            params MetadataReference[] additionalReferences)
        {
            var file = new FileInfo(path);
            var parseOptions = GetParseOptionsAlternatives(options, file);

            using (var workspace = new AdhocWorkspace())
            {
                var document = GetDocument(file, GeneratedAssemblyName, workspace, additionalReferences);
                var project = document.Project;

                foreach (var parseOption in parseOptions)
                {
                    if (parseOption != null)
                    {
                        project = project.WithParseOptions(parseOption);
                    }

                    var compilation = project.GetCompilationAsync().Result;
                    var diagnostics = GetDiagnostics(compilation, diagnosticAnalyzer);
                    var expected = ExpectedIssues(compilation.SyntaxTrees.First()).ToList();

                    foreach (var diagnostic in diagnostics)
                    {
                        var line = diagnostic.GetLineNumberToReport();
                        expected.Should().Contain(line);
                        expected.Remove(line);
                    }

                    expected.Should().BeEquivalentTo(Enumerable.Empty<int>());
                }
            }
        }
 protected virtual void TestMissing(
     string initial,
     ParseOptions parseOptions,
     Func<dynamic, dynamic> nodeLocator = null)
 {
     TestMissing(initial, parseOptions, null, nodeLocator);
 }
        private void TestAnnotations(
            string expectedText,
            IList<TextSpan> expectedSpans,
            SyntaxNode fixedRoot,
            string annotationKind,
            bool compareTokens,
            ParseOptions parseOptions = null)
        {
            expectedSpans = expectedSpans ?? new List<TextSpan>();
            var annotatedTokens = fixedRoot.GetAnnotatedNodesAndTokens(annotationKind).Select(n => (SyntaxToken)n).ToList();

            Assert.Equal(expectedSpans.Count, annotatedTokens.Count);

            if (expectedSpans.Count > 0)
            {
                var expectedTokens = TokenUtilities.GetTokens(TokenUtilities.GetSyntaxRoot(expectedText, GetLanguage(), parseOptions));
                var actualTokens = TokenUtilities.GetTokens(fixedRoot);

                for (var i = 0; i < Math.Min(expectedTokens.Count, actualTokens.Count); i++)
                {
                    var expectedToken = expectedTokens[i];
                    var actualToken = actualTokens[i];

                    var actualIsConflict = annotatedTokens.Contains(actualToken);
                    var expectedIsConflict = expectedSpans.Contains(expectedToken.Span);
                    Assert.Equal(expectedIsConflict, actualIsConflict);
                }
            }
        }
Exemple #5
0
        public static DocumentState Create(
            DocumentInfo info,
            ParseOptions options,
            HostLanguageServices language,
            SolutionServices services)
        {
            var textSource = info.TextLoader != null
                ? CreateRecoverableText(info.TextLoader, info.Id, services)
                : CreateStrongText(TextAndVersion.Create(SourceText.From(string.Empty, Encoding.UTF8), VersionStamp.Default, info.FilePath));

            var treeSource = CreateLazyFullyParsedTree(
                textSource,
                GetSyntaxTreeFilePath(info),
                options,
                languageServices: language);

            // remove any initial loader so we don't keep source alive
            info = info.WithTextLoader(null);

            return new DocumentState(
                languageServices: language,
                solutionServices: services,
                info: info,
                options: options,
                textSource: textSource,
                treeSource: treeSource);
        }
 /// <param name="files">Can pass in multiple file contents with individual source kind: files will be named test1.vb, test2.vbx, etc.</param>
 public static Task<TestWorkspace> CreateWorkspaceFromFilesAsync(
     string[] files,
     ParseOptions[] parseOptions = null,
     CompilationOptions compilationOptions = null,
     ExportProvider exportProvider = null)
 {
     return TestWorkspaceFactory.CreateWorkspaceFromFilesAsync(LanguageNames.VisualBasic, compilationOptions, parseOptions, files, exportProvider);
 }
Exemple #7
0
 public TestHostSolution(
     HostLanguageServices languageServiceProvider,
     CompilationOptions compilationOptions,
     ParseOptions parseOptions,
     params MetadataReference[] references)
     : this(new TestHostProject(languageServiceProvider, compilationOptions, parseOptions, references))
 {
 }
 private static CodeGenerationOptions GetCodeGenerationOptions(
     EnvDTE.vsCMAccess access, ParseOptions parseOptions)
 {
     var generateDefaultAccessibility = (access & EnvDTE.vsCMAccess.vsCMAccessDefault) == 0;
     return new CodeGenerationOptions(
         generateDefaultAccessibility: generateDefaultAccessibility,
         parseOptions: parseOptions);
 }
 private void SetOptionsCore(CompilationOptions newCompilationOptions, ParseOptions newParseOptions)
 {
     lock (_gate)
     {
         _currentCompilationOptions = newCompilationOptions;
         _currentParseOptions = newParseOptions;
     }
 }
 protected void TestMissing(
     string initialMarkup,
     ParseOptions parseOptions,
     IDictionary<OptionKey, object> options = null,
     string fixAllActionEquivalenceKey = null)
 {
     TestMissing(initialMarkup, parseOptions, compilationOptions: null, options:options, fixAllActionEquivalenceKey: fixAllActionEquivalenceKey);
 }
        protected static void CommonGetObjectData(ParseOptions options, SerializationInfo info, StreamingContext context)
        {
            //public readonly SourceCodeKind Kind;
            info.AddValue("Kind", options.Kind, typeof(SourceCodeKind));

            //public readonly DocumentationMode DocumentationMode;
            info.AddValue("DocumentationMode", options.DocumentationMode, typeof(DocumentationMode));
        }
 /// <param name="files">Can pass in multiple file contents: files will be named test1.vb, test2.vb, etc. and additional metadata references</param>
 public static TestWorkspace CreateWorkspaceFromFiles(
     string[] files,
     ParseOptions parseOptions = null,
     CompilationOptions compilationOptions = null,
     ExportProvider exportProvider = null,
     string[] metadataReferences = null)
 {
     return TestWorkspaceFactory.CreateWorkspaceFromFiles(LanguageNames.VisualBasic, compilationOptions, parseOptions, files, exportProvider, metadataReferences);
 }
 public static Task<TestWorkspace> CreateWorkspaceFromFileAsync(
     string file,
     ParseOptions parseOptions = null,
     CompilationOptions compilationOptions = null,
     ExportProvider exportProvider = null,
     string[] metadataReferences = null)
 {
     return CreateWorkspaceFromFilesAsync(new[] { file }, parseOptions, compilationOptions, exportProvider, metadataReferences);
 }
 protected Task TestMissingAsync(
     string initialMarkup,
     ParseOptions parseOptions,
     IDictionary<OptionKey, object> options = null,
     string fixAllActionEquivalenceKey = null,
     object fixProviderData = null)
 {
     return TestMissingAsync(initialMarkup, parseOptions, compilationOptions: null, options: options, fixAllActionEquivalenceKey: fixAllActionEquivalenceKey, fixProviderData: fixProviderData);
 }
 protected void Test(
     string initial,
     string expected,
     ParseOptions parseOptions,
     int index = 0,
     bool compareTokens = true,
     Func<dynamic, dynamic> nodeLocator = null,
     IDictionary<OptionKey, object> options = null)
 {
     Test(initial, expected, parseOptions, null, index, compareTokens, nodeLocator, options);
 }
 private static ValueSource<TreeAndVersion> CreateLazyFullyParsedTree(
     ValueSource<TextAndVersion> newTextSource,
     string filePath,
     ParseOptions options,
     HostLanguageServices languageServices,
     PreservationMode mode = PreservationMode.PreserveValue)
 {
     return new AsyncLazy<TreeAndVersion>(
         c => FullyParseTreeAsync(newTextSource, filePath, options, languageServices, mode, c),
         cacheResult: true);
 }
Exemple #17
0
 internal static SyntaxNode GetSyntaxRoot(string expectedText, string language, ParseOptions options = null)
 {
     if (language == LanguageNames.CSharp)
     {
         return CS.SyntaxFactory.ParseCompilationUnit(expectedText, options: (CS.CSharpParseOptions)options);
     }
     else
     {
         return VB.SyntaxFactory.ParseCompilationUnit(expectedText, options: (VB.VisualBasicParseOptions)options);
     }
 }
        protected bool EqualsHelper(ParseOptions other)
        {
            if (object.ReferenceEquals(other, null))
            {
                return false;
            }

            return
                this.Kind == other.Kind &&
                this.DocumentationMode == other.DocumentationMode &&
                (this.PreprocessorSymbolNames == null ? other.PreprocessorSymbolNames == null : this.PreprocessorSymbolNames.SequenceEqual(other.PreprocessorSymbolNames, StringComparer.Ordinal));
        }
 protected void TestMissing(
     string initialMarkup,
     ParseOptions parseOptions,
     CompilationOptions compilationOptions,
     Func<dynamic, dynamic> nodeLocator = null)
 {
     using (var workspace = CreateWorkspaceFromFile(initialMarkup, parseOptions, compilationOptions))
     {
         var codeIssueOrRefactoring = GetCodeRefactoring(workspace, nodeLocator);
         Assert.Null(codeIssueOrRefactoring);
     }
 }
Exemple #20
0
 private DocumentState(
     HostLanguageServices languageServices,
     SolutionServices solutionServices,
     DocumentInfo info,
     ParseOptions options,
     ValueSource<TextAndVersion> textSource,
     ValueSource<TreeAndVersion> treeSource)
     : base(solutionServices, info, textSource)
 {
     _languageServices = languageServices;
     _options = options;
     _treeSource = treeSource;
 }
        /// <summary>
        /// Sets the given compilation and parse options.
        /// </summary>
        protected void SetOptions(CompilationOptions newCompilationOptions, ParseOptions newParseOptions)
        {
            this.UpdateRuleSetError(this.RuleSetFile);

            // Set options.
            CurrentCompilationOptions = newCompilationOptions;
            CurrentParseOptions = newParseOptions;

            if (_pushingChangesToWorkspaceHosts)
            {
                this.ProjectTracker.NotifyWorkspaceHosts(host => host.OnOptionsChanged(Id, newCompilationOptions, newParseOptions));
            }
        }
        protected async Task TestMissingAsync(
            string initialMarkup,
            ParseOptions parseOptions,
            IDictionary<OptionKey, object> options = null,
            string fixAllActionEquivalenceKey = null,
            object fixProviderData = null,
            bool withScriptOption = false)
        {
            await TestMissingAsync(initialMarkup, parseOptions, compilationOptions: null, options: options, fixAllActionEquivalenceKey: fixAllActionEquivalenceKey, fixProviderData: fixProviderData);

            if (withScriptOption)
            {
                await TestMissingAsync(initialMarkup, parseOptions.WithKind(SourceCodeKind.Script), compilationOptions: null, options: options, fixAllActionEquivalenceKey: fixAllActionEquivalenceKey, fixProviderData: fixProviderData);
            }
        }
Exemple #23
0
 private DocumentState(
     HostLanguageServices languageServices,
     SolutionServices solutionServices,
     DocumentInfo info,
     ParseOptions options,
     ValueSource<TextAndVersion> textSource,
     ValueSource<TreeAndVersion> treeSource)
 {
     this.languageServices = languageServices;
     this.solutionServices = solutionServices;
     this.info = info;
     this.options = options;
     this.textSource = textSource;
     this.treeSource = treeSource;
 }
        private static ParseOptions WithPreprocessorSymbols(ParseOptions parseOptions, List<string> symbols)
        {
            var csharpParseOptions = parseOptions as CSharpParseOptions;
            if (csharpParseOptions != null)
            {
                return csharpParseOptions.WithPreprocessorSymbols(symbols);
            }

            var basicParseOptions = parseOptions as VisualBasicParseOptions;
            if (basicParseOptions != null)
            {
                return basicParseOptions.WithPreprocessorSymbols(symbols.Select(x => new KeyValuePair<string, object>(x, true)));
            }

            throw new NotSupportedException();
        }
        public RoslynScriptEngine(SparkContext sc, SqlContext sqlContext)
        {
            this.sc = sc;
            sparkConf = sc.GetConf();
            host = new SparkCLRHost
            {
                sc = sc,
                sqlContext = sqlContext
            };

            var sparkLocalDir = sparkConf.Get("spark.local.dir", Path.GetTempPath());
            compilationDumpDirectory = Path.Combine(sparkLocalDir, Path.GetRandomFileName());
            Directory.CreateDirectory(compilationDumpDirectory);

            options = new CSharpParseOptions(LanguageVersion.CSharp6, DocumentationMode.Parse, SourceCodeKind.Script);
        }
            public HostProject(Workspace workspace, SolutionId solutionId, string languageName, ParseOptions parseOptions, IEnumerable<MetadataReference> metadataReferences)
            {
                Debug.Assert(workspace != null);
                Debug.Assert(languageName != null);
                Debug.Assert(parseOptions != null);
                Debug.Assert(metadataReferences != null);

                _workspace = workspace;
                this.Id = ProjectId.CreateNewId(debugName: "Miscellaneous Files");
                this.Language = languageName;
                _parseOptions = parseOptions;

                // the assembly name must be unique for each collection of loose files.  since the name doesn't matter
                // a random GUID can be used.
                _assemblyName = Guid.NewGuid().ToString("N");

                _version = VersionStamp.Create();
                _metadataReferences = metadataReferences;
            }
Exemple #27
0
        private DocumentState(
            HostLanguageServices languageServices,
            SolutionServices solutionServices,
            DocumentInfo info,
            ParseOptions options,
            ValueSource<TextAndVersion> textSource,
            ValueSource<TreeAndVersion> treeSource)
            : base(solutionServices, info, textSource)
        {
            _languageServices = languageServices;
            _options = options;

            // If this is document that doesn't support syntax, then don't even bother holding
            // onto any tree source.  It will never be used to get a tree, and can only hurt us
            // by possibly holding onto data that might cause a slow memory leak.
            _treeSource = this.SupportsSyntaxTree
                ? treeSource
                : ValueSource<TreeAndVersion>.Empty;
        }
        /// <summary>
        /// Creates new compilation options from parsed command line arguments, with additional workspace specific options appended.
        /// It is expected that derived types which need to add more specific options will fetch the base options and override those options.
        /// </summary>
        protected virtual CompilationOptions CreateCompilationOptions(CommandLineArguments commandLineArguments, ParseOptions newParseOptions)
        {
            Contract.ThrowIfNull(commandLineArguments);

            // Get options from command line arguments.
            var options = commandLineArguments.CompilationOptions;

            // Now set the default workspace options (these are not set by the command line parser).
            string projectDirectory = this.ContainingDirectoryPathOpt;

            // TODO: #r support, should it include bin path?
            var referenceSearchPaths = ImmutableArray<string>.Empty;

            // TODO: #load support
            var sourceSearchPaths = ImmutableArray<string>.Empty;

            MetadataReferenceResolver referenceResolver;
            if (Workspace != null)
            {
                referenceResolver = new WorkspaceMetadataFileReferenceResolver(
                    Workspace.CurrentSolution.Services.MetadataService,
                    new RelativePathResolver(referenceSearchPaths, projectDirectory));
            }
            else
            {
                // can only happen in tests
                referenceResolver = null;
            }

            // Explicitly disable concurrent build.
            options = options.WithConcurrentBuild(concurrent: false);

            // Set default resolvers.
            options = options.WithMetadataReferenceResolver(referenceResolver)
                .WithXmlReferenceResolver(new XmlFileResolver(projectDirectory))
                .WithSourceReferenceResolver(new SourceFileResolver(sourceSearchPaths, projectDirectory))
                .WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default)
                .WithStrongNameProvider(new DesktopStrongNameProvider(GetStrongNameKeyPaths()));

            return options;
        }
        protected async Task TestAsync(string markup, string expectedCode, ParseOptions options = null)
        {
            using (var workspace = await CreateWorkspaceFromCodeAsync(markup, options))
            {
                var position = workspace.Documents.Single().CursorPosition.Value;
                var document = GetDocument(workspace);
                var braceMatcher = workspace.GetService<IBraceMatchingService>();

                var foundSpan = await braceMatcher.FindMatchingSpanAsync(document, position, CancellationToken.None);
                MarkupTestFile.GetSpans(expectedCode, out var parsedExpectedCode, out IList<TextSpan> expectedSpans);

                if (expectedSpans.Any())
                {
                    Assert.Equal(expectedSpans.Single(), foundSpan.Value);
                }
                else
                {
                    Assert.False(foundSpan.HasValue);
                }
            }
        }
Exemple #30
0
 private ProjectInfo(
     ProjectAttributes attributes,
     CompilationOptions compilationOptions,
     ParseOptions parseOptions,
     IEnumerable<DocumentInfo> documents,
     IEnumerable<ProjectReference> projectReferences,
     IEnumerable<MetadataReference> metadataReferences,
     IEnumerable<AnalyzerReference> analyzerReferences,
     IEnumerable<DocumentInfo> additionalDocuments,
     Type hostObjectType)
 {
     Attributes = attributes;
     CompilationOptions = compilationOptions;
     ParseOptions = parseOptions;
     Documents = documents.ToImmutableReadOnlyListOrEmpty();
     ProjectReferences = projectReferences.ToImmutableReadOnlyListOrEmpty();
     MetadataReferences = metadataReferences.ToImmutableReadOnlyListOrEmpty();
     AnalyzerReferences = analyzerReferences.ToImmutableReadOnlyListOrEmpty();
     AdditionalDocuments = additionalDocuments.ToImmutableReadOnlyListOrEmpty();
     HostObjectType = hostObjectType;
 }
        private ProjectInfo With(
            ProjectId id                                       = null,
            VersionStamp?version                               = default(VersionStamp?),
            string name                                        = null,
            string assemblyName                                = null,
            string language                                    = null,
            Optional <string> filePath                         = default(Optional <string>),
            Optional <string> outputPath                       = default(Optional <string>),
            CompilationOptions compilationOptions              = null,
            ParseOptions parseOptions                          = null,
            IEnumerable <DocumentInfo> documents               = null,
            IEnumerable <ProjectReference> projectReferences   = null,
            IEnumerable <MetadataReference> metadataReferences = null,
            Optional <bool> isSubmission                       = default(Optional <bool>),
            Optional <Type> hostObjectType                     = default(Optional <Type>))
        {
            var newId                 = id ?? this.Id;
            var newVersion            = version.HasValue ? version.Value : this.Version;
            var newName               = name ?? this.Name;
            var newAssemblyName       = assemblyName ?? this.AssemblyName;
            var newLanguage           = language ?? this.Language;
            var newFilepath           = filePath.HasValue ? filePath.Value : this.FilePath;
            var newOutputPath         = outputPath.HasValue ? outputPath.Value : this.OutputFilePath;
            var newCompilationOptions = compilationOptions ?? this.CompilationOptions;
            var newParseOptions       = parseOptions ?? this.ParseOptions;
            var newDocuments          = documents ?? this.Documents;
            var newProjectReferences  = projectReferences ?? this.ProjectReferences;
            var newMetadataReferences = metadataReferences ?? this.MetadataReferences;
            var newIsSubmission       = isSubmission.HasValue ? isSubmission.Value : this.IsSubmission;
            var newHostObjectType     = hostObjectType.HasValue ? hostObjectType.Value : this.HostObjectType;

            if (newId == this.Id &&
                newVersion == this.Version &&
                newName == this.Name &&
                newAssemblyName == this.AssemblyName &&
                newLanguage == this.Language &&
                newFilepath == this.FilePath &&
                newOutputPath == this.OutputFilePath &&
                newCompilationOptions == this.CompilationOptions &&
                newParseOptions == this.ParseOptions &&
                newDocuments == this.Documents &&
                newProjectReferences == this.ProjectReferences &&
                newMetadataReferences == this.MetadataReferences &&
                newIsSubmission == this.IsSubmission &&
                newHostObjectType == this.HostObjectType)
            {
                return(this);
            }

            return(new ProjectInfo(
                       newId,
                       newVersion,
                       newName,
                       newAssemblyName,
                       newLanguage,
                       newFilepath,
                       newOutputPath,
                       newCompilationOptions,
                       newParseOptions,
                       newDocuments,
                       newProjectReferences,
                       newMetadataReferences,
                       newIsSubmission,
                       newHostObjectType));
        }
Exemple #32
0
 /// <summary>
 /// Creates a new instance of this project updated to have the specified parse options.
 /// </summary>
 public Project WithParseOptions(ParseOptions options)
 {
     return(this.Solution.WithProjectParseOptions(this.Id, options).GetProject(this.Id));
 }
Exemple #33
0
 /// <summary>
 /// Returns a new tree whose root and options are as specified and other properties are copied from the current tree.
 /// </summary>
 public abstract SyntaxTree WithRootAndOptions(SyntaxNode root, ParseOptions options);
 internal GeneratorDriver(ParseOptions parseOptions, ImmutableArray <ISourceGenerator> generators, AnalyzerConfigOptionsProvider optionsProvider, ImmutableArray <AdditionalText> additionalTexts, bool enableIncremental)
 {
     (var filteredGenerators, var incrementalGenerators) = GetIncrementalGenerators(generators, enableIncremental);
     _state = new GeneratorDriverState(parseOptions, optionsProvider, filteredGenerators, incrementalGenerators, additionalTexts, ImmutableArray.Create(new GeneratorState[filteredGenerators.Length]), DriverStateTable.Empty, enableIncremental);
 }
Exemple #35
0
 public ProjectInfo WithParseOptions(ParseOptions parseOptions)
 {
     return(With(parseOptions: parseOptions));
 }
Exemple #36
0
        public SourceGeneratedDocumentState WithUpdatedGeneratedContent(SourceText sourceText, SyntaxTree lazySyntaxTree, ParseOptions parseOptions, CancellationToken cancellationToken)
        {
            if (TryGetText(out var existingText) &&
                Checksum.From(existingText.GetChecksum()) == Checksum.From(sourceText.GetChecksum()) &&
                SyntaxTree.Options.Equals(parseOptions))
            {
                // We can reuse this instance directly
                return(this);
            }

            return(SourceGeneratedDocumentState.Create(
                       this.HintName,
                       sourceText,
                       lazySyntaxTree,
                       this.Id,
                       this.SourceGenerator,
                       this.LanguageServices,
                       this.solutionServices,
                       cancellationToken));
        }
Exemple #37
0
 public GeneratorDriver WithUpdatedParseOptions(ParseOptions newOptions) => newOptions is object
 ?FromState(_state.With(parseOptions: newOptions))
Exemple #38
0
 internal GeneratorDriver(ParseOptions parseOptions, ImmutableArray <ISourceGenerator> generators, AnalyzerConfigOptionsProvider optionsProvider, ImmutableArray <AdditionalText> additionalTexts, GeneratorDriverOptions driverOptions)
 {
     (var filteredGenerators, var incrementalGenerators) = GetIncrementalGenerators(generators, SourceExtension);
     _state = new GeneratorDriverState(parseOptions, optionsProvider, filteredGenerators, incrementalGenerators, additionalTexts, ImmutableArray.Create(new GeneratorState[filteredGenerators.Length]), DriverStateTable.Empty, SyntaxStore.Empty, driverOptions.DisabledOutputs, runtime: TimeSpan.Zero, driverOptions.TrackIncrementalGeneratorSteps);
 }
Exemple #39
0
        public SourceGeneratedDocumentState WithUpdatedGeneratedContent(SourceText sourceText, ParseOptions parseOptions)
        {
            if (TryGetText(out var existingText) &&
                Checksum.From(existingText.GetChecksum()) == Checksum.From(sourceText.GetChecksum()) &&
                ParseOptions.Equals(parseOptions))
            {
                // We can reuse this instance directly
                return(this);
            }

            return(Create(
                       Identity,
                       sourceText,
                       parseOptions,
                       this.LanguageServices,
                       this.solutionServices));
        }