Example #1
0
        public static UInt256 CalculateMerkleRoot(ImmutableArray<Transaction> transactions, out ImmutableArray<ImmutableArray<byte>> merkleTree)
        {
            var workingMerkleTree = new List<ImmutableArray<byte>>();

            var hashes = transactions.Select(tx => tx.Hash.ToByteArray().ToImmutableArray()).ToList();

            workingMerkleTree.AddRange(hashes);
            while (hashes.Count > 1)
            {
                workingMerkleTree.AddRange(hashes);

                // ensure row is even length
                if (hashes.Count % 2 != 0)
                    hashes.Add(hashes.Last());

                // pair up hashes in row ({1, 2, 3, 4} into {{1, 2}, {3, 4}}) and then hash the pairs
                // the result is the next row, which will be half the size of the current row
                hashes =
                    Enumerable.Range(0, hashes.Count / 2)
                    .Select(i => hashes[i * 2].AddRange(hashes[i * 2 + 1]))
                    //.AsParallel().AsOrdered().WithExecutionMode(ParallelExecutionMode.ForceParallelism).WithDegreeOfParallelism(10)
                    .Select(pair => Crypto.DoubleSHA256(pair.ToArray()).ToImmutableArray())
                    .ToList();
            }
            Debug.Assert(hashes.Count == 1);

            merkleTree = workingMerkleTree.ToImmutableArray();
            return new UInt256(hashes[0].ToArray());
        }
Example #2
0
        private ImmutableArray<TodoCommentDescriptor> Parse(string data)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                return ImmutableArray<TodoCommentDescriptor>.Empty;
            }

            var tuples = data.Split('|');
            var result = new List<TodoCommentDescriptor>(tuples.Length);

            foreach (var tuple in tuples)
            {
                if (string.IsNullOrWhiteSpace(tuple))
                {
                    continue;
                }

                var pair = tuple.Split(':');

                if (pair.Length != 2 || string.IsNullOrWhiteSpace(pair[0]))
                {
                    continue;
                }

                int priority;
                if (!int.TryParse(pair[1], NumberStyles.None, CultureInfo.InvariantCulture, out priority))
                {
                    continue;
                }

                result.Add(new TodoCommentDescriptor(pair[0].Trim(), priority));
            }

            return result.ToImmutableArray();
        }
                private ImmutableArray<int> ComputePathFromRoot(SyntaxNode node)
                {
                    var path = new List<int>();
                    var root = _tree.GetRoot();

                    while (node != root)
                    {
                        for (; node.Parent != null; node = node.Parent)
                        {
                            var index = GetChildIndex(node);
                            path.Add(index);
                        }

                        // if we were part of structure trivia, continue searching until we get to the true root
                        if (node.IsStructuredTrivia)
                        {
                            var trivia = node.ParentTrivia;
                            var triviaIndex = GetTriviaIndex(trivia);
                            path.Add(triviaIndex);
                            var tokenIndex = GetChildIndex(trivia.Token);
                            path.Add(tokenIndex);
                            node = trivia.Token.Parent;
                            continue;
                        }
                        else if (node != root)
                        {
                            throw new InvalidOperationException(CSharpWorkspaceResources.Node_does_not_descend_from_root);
                        }
                    }

                    path.Reverse();
                    return path.ToImmutableArray();
                }
		internal static ImmutableArray<TypeToRemove> GetTypesToRemove(
			this SyntaxNode @this, SemanticModel model, 
			string documentFileNameWithoutExtension)
		{
			var typesToRemove = new List<TypeToRemove>();
			TypeDeclarationSyntax typeToPreserve = null;

			var typeNodes = @this.DescendantNodes(_ => true)
				.OfType<TypeDeclarationSyntax>();

			foreach(var typeNode in typeNodes)
			{
				var type = model.GetDeclaredSymbol(typeNode) as ITypeSymbol;

				if(type.ContainingType == null)
				{
					if(type.Name != documentFileNameWithoutExtension)
					{
						typesToRemove.Add(new TypeToRemove(
							typeNode, type));
					}
					else
					{
						typeToPreserve = typeNode;
					}
				}
			}

			return typesToRemove.ToImmutableArray();
		}
        public SymbolImplementation BuildMethod(SourceMethodDefinitionSymbol methodSymbol)
        {
            var methodBody = methodSymbol.Syntax.Body;
            var symbolTable = (ISymbolTable) methodSymbol.Parent;

            _rootScope = new SymbolScope(symbolTable);
            _currentScope = _rootScope;

            var symbolContext = methodSymbol;
            var implementationNode = methodBody;

            var statements = new List<BoundStatement>();
            var statementBuilder = new StatementBinder(this, symbolContext, _diagnostics);

            if (symbolContext.Parameters != null)
            {
                int parameterCount = symbolContext.Parameters.Length;
                for (int paramIndex = 0; paramIndex < parameterCount; paramIndex++)
                    _currentScope.AddSymbol(symbolContext.Parameters[paramIndex]);
            }

            foreach (var statementNode in implementationNode.Statements)
            {
                var statement = statementBuilder.BuildStatement(statementNode);
                if (statement != null)
                    statements.Add(statement);
            }

            return new SymbolImplementation(statements.ToImmutableArray(), _rootScope);
        }
Example #6
0
        internal ArenaBehavior(World world, List<Team> teams)
        {
            Requires.NotNull(world, nameof(world));
            Requires.NotNull(teams, nameof(teams));

            this.world = world;
            this.teams = teams.ToImmutableArray();
        }
Example #7
0
        public void ConcurrentAccess()
        {
            var source =
@"class A
{
    B F;
    D P { get; set; }
    void M(A a, B b, S s, I i) { }
    delegate void D(S s);
    class B { }
    struct S { }
    interface I { }
}
class B
{
    A M<T, U>() where T : A where U : T, I { return null; }
    event D E;
    delegate void D(S s);
    struct S { }
    interface I { }
}";

            var compilation0 = CreateCompilationWithMscorlib(source, options: TestOptions.DebugDll);
            var compilation1 = compilation0.WithSource(source);

            var builder = new List<Symbol>();
            var type = compilation1.GetMember<NamedTypeSymbol>("A");
            builder.Add(type);
            builder.AddRange(type.GetMembers());
            type = compilation1.GetMember<NamedTypeSymbol>("B");
            builder.Add(type);
            builder.AddRange(type.GetMembers());
            var members = builder.ToImmutableArray();
            Assert.True(members.Length > 10);

            for (int i = 0; i < 10; i++)
            {
                var matcher = new CSharpSymbolMatcher(
                    null,
                    compilation1.SourceAssembly,
                    default(EmitContext),
                    compilation0.SourceAssembly,
                    default(EmitContext),
                    null);

                var tasks = new Task[10];
                for (int j = 0; j < tasks.Length; j++)
                {
                    int startAt = i + j + 1;
                    tasks[j] = Task.Run(() =>
                    {
                        MatchAll(matcher, members, startAt);
                        Thread.Sleep(10);
                    });
                }
                Task.WaitAll(tasks);
            }
        }
Example #8
0
        private BoundNode BindTypedefStatement(TypedefStatementSyntax declaration)
        {
            var boundType = Bind(declaration.Type, x => BindType(x, null));

            var boundDeclarations = new List<BoundTypeAlias>();
            foreach (var declarator in declaration.Declarators)
            {
                boundDeclarations.Add(Bind(declarator, x => BindTypeAlias(x, boundType.TypeSymbol)));
            }

            return new BoundTypedefStatement(boundDeclarations.ToImmutableArray());
        }
Example #9
0
        /// <summary>
        /// Updates the general options.
        /// </summary>
        /// <param name="disableRuleGroups">
        /// The rules that can be turned off for the formatter.
        /// </param>
        /// <param name="tabSize">
        /// How big in spaces the indents are when you format
        /// </param>
        /// <param name="indentSize">
        /// How big in spaces the indents are when you press tab,
        /// </param>
        /// <param name="usingTabs">
        /// Whether or not Keep Tabs is on or off.
        /// </param>
        public FormattingOptions(
            List<DisableableRules> disableRuleGroups,
            uint tabSize, uint indentSize, bool usingTabs)
        {
            Requires.NotNull(disableRuleGroups, nameof(disableRuleGroups));

            this.TabSize = tabSize;
            this.IndentSize = indentSize;
            this.UsingTabs = usingTabs;
            this.RuleGroupsToDisable = disableRuleGroups.ToImmutableArray();
            this.OptionalRuleMap = new OptionalRuleMap(this.RuleGroupsToDisable);
        }
Example #10
0
        static IntrinsicSemantics()
        {
            var allSemantics = new List<SemanticSymbol>();

            // D3D10
            allSemantics.Add(new SemanticSymbol("SV_ClipDistance", "Clip distance data. SV_ClipDistance values are each assumed to be a float32 signed distance to a plane. Primitive setup only invokes rasterization on pixels for which the interpolated plane distance(s) are >= 0. Multiple clip planes can be implemented simultaneously, by declaring multiple component(s) of one or more vertex elements as the SV_ClipDistance. The combined clip and cull distance values are at most D3D#_CLIP_OR_CULL_DISTANCE_COUNT components in at most D3D#_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT registers.", true, SemanticUsages.AllShaders & ~SemanticUsages.VertexShaderInput, IntrinsicTypes.Float));
            allSemantics.Add(new SemanticSymbol("SV_CullDistance", "Cull distance data. When component(s) of vertex Element(s) are given this label, these values are each assumed to be a float32 signed distance to a plane. Primitives will be completely discarded if the plane distance(s) for all of the vertices in the primitive are < 0. Multiple cull planes can be used simultaneously, by declaring multiple component(s) of one or more vertex elements as the SV_CullDistance. The combined clip and cull distance values are at most D3D#_CLIP_OR_CULL_DISTANCE_COUNT components in at most D3D#_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT registers.", true, SemanticUsages.AllShaders & ~SemanticUsages.VertexShaderInput, IntrinsicTypes.Float));
            allSemantics.Add(new SemanticSymbol("SV_Coverage", "A mask that can be specified on input, output, or both of a pixel shader.\n\nFor SV_Coverage on a pixel shader, OUTPUT is supported on ps_4_1 or higher.\n\nFor SV_Coverage on a pixel shader, INPUT requires ps_5_0 or higher.", false, SemanticUsages.PixelShaderInput | SemanticUsages.PixelShaderOutput, IntrinsicTypes.Uint));
            allSemantics.Add(new SemanticSymbol("SV_Depth", "Depth buffer data.", false, SemanticUsages.AllShaders, IntrinsicTypes.Float));
            allSemantics.Add(new SemanticSymbol("SV_DepthGreaterEqual", "Tests whether the value is greater than or equal to the depth data value.", true, SemanticUsages.PixelShaderOutput));
            allSemantics.Add(new SemanticSymbol("SV_DepthLessEqual", "Tests whether the value is less than or equal to the depth data value.", true, SemanticUsages.PixelShaderOutput));
            allSemantics.Add(new SemanticSymbol("SV_DispatchThreadID", "Defines the global thread offset within the Dispatch call, per dimension of the group. (read only)", false, SemanticUsages.ComputeShaderInput, IntrinsicTypes.Uint3));
            allSemantics.Add(new SemanticSymbol("SV_DomainLocation", "Defines the location on the hull of the current domain point being evaluated. (read only)", false, SemanticUsages.DomainShaderInput, IntrinsicTypes.Float2, IntrinsicTypes.Float3));
            allSemantics.Add(new SemanticSymbol("SV_GroupID", "Defines the group offset within a Dispatch call, per dimension of the dispatch call. (read only)", false, SemanticUsages.ComputeShaderInput, IntrinsicTypes.Uint3));
            allSemantics.Add(new SemanticSymbol("SV_GroupIndex", "Provides a flattened index for a given thread within a given group. (read only)", false, SemanticUsages.ComputeShaderInput, IntrinsicTypes.Uint));
            allSemantics.Add(new SemanticSymbol("SV_GroupThreadID", "Defines the thread offset within the group, per dimension of the group. (read only)", false, SemanticUsages.ComputeShaderInput, IntrinsicTypes.Uint3));
            allSemantics.Add(new SemanticSymbol("SV_GSInstanceID", "Defines the instance of the geometry shader. The instance is needed as a geometry shader can be invoked up to 32 times on the same geometry primitive.", false, SemanticUsages.GeometryShaderInput, IntrinsicTypes.Uint));
            allSemantics.Add(new SemanticSymbol("SV_InnerCoverage", "Represents underestimated conservative rasterization information (i.e. whether a pixel is guaranteed-to-be-fully covered).", false, SemanticUsages.PixelShaderInput | SemanticUsages.PixelShaderOutput));
            allSemantics.Add(new SemanticSymbol("SV_InsideTessFactor", "Defines the tessellation amount within a patch surface.", false, SemanticUsages.HullShaderOutput | SemanticUsages.DomainShaderInput, IntrinsicTypes.Float, IntrinsicTypes.Float2));
            allSemantics.Add(new SemanticSymbol("SV_InstanceID", "Per-instance identifier automatically generated by the runtime.", false, SemanticUsages.VertexShaderInput));
            allSemantics.Add(new SemanticSymbol("SV_IsFrontFace", "Specifies whether a triangle is front facing. For lines and points, IsFrontFace has the value true. The exception is lines drawn out of triangles (wireframe mode), which sets IsFrontFace the same way as rasterizing the triangle in solid mode.", false, SemanticUsages.GeometryShaderOutput | SemanticUsages.PixelShaderInput, IntrinsicTypes.Bool));
            allSemantics.Add(new SemanticSymbol("SV_OutputControlPointID", "Defines the index of the control point ID being operated on by an invocation of the main entry point of the hull shader.", false, SemanticUsages.HullShaderInput, IntrinsicTypes.Uint));
            allSemantics.Add(new SemanticSymbol("SV_Position", "When SV_Position is declared for input to a shader, it can have one of two interpolation modes specified: linearNoPerspective or linearNoPerspectiveCentroid, where the latter causes centroid-snapped xyzw values to be provided when multisample antialiasing. When used in a shader, SV_Position describes the pixel location. Available in all shaders to get the pixel center with a 0.5 offset.", false, SemanticUsages.VertexShaderOutput | SemanticUsages.GeometryShaderInput | SemanticUsages.GeometryShaderOutput | SemanticUsages.PixelShaderInput, IntrinsicTypes.Float4));
            allSemantics.Add(new SemanticSymbol("SV_PrimitiveID", "Per-primitive identifier automatically generated by the runtime.", false, SemanticUsages.GeometryShaderOutput | SemanticUsages.PixelShaderOutput | SemanticUsages.GeometryShaderInput | SemanticUsages.PixelShaderInput | SemanticUsages.HullShaderInput | SemanticUsages.DomainShaderInput, IntrinsicTypes.Uint));
            allSemantics.Add(new SemanticSymbol("SV_RenderTargetArrayIndex", "Render-target array index. Applied to geometry shader output and indicates the render target array slice that the primitive will be drawn to by the pixel shader. SV_RenderTargetArrayIndex is only valid if the render target is an array resource. This semantic applies only to primitives, if a primitive has more than one vertex the value from the leading vertex will be used.\nThis value also indicates which array slice of a depthstencilview is used for read / write purposes.", false, SemanticUsages.GeometryShaderOutput | SemanticUsages.PixelShaderInput | SemanticUsages.PixelShaderOutput, IntrinsicTypes.Uint));
            allSemantics.Add(new SemanticSymbol("SV_SampleIndex", "Sample frequency index data.", false, SemanticUsages.PixelShaderInput | SemanticUsages.PixelShaderOutput, IntrinsicTypes.Uint));
            allSemantics.Add(new SemanticSymbol("SV_StencilRef", "Represents the current pixel shader stencil reference value.", false, SemanticUsages.PixelShaderOutput, IntrinsicTypes.Uint));
            allSemantics.Add(new SemanticSymbol("SV_Target", "The output value that will be stored in a render target. The index indicates which of the 8 possibly bound render targets to write to.", true, SemanticUsages.PixelShaderOutput, IntrinsicTypes.Float));
            allSemantics.Add(new SemanticSymbol("SV_TessFactor", "Defines the tessellation amount on each edge of a patch. Available for writing in the hull shader and reading in the domain shader.", false, SemanticUsages.HullShaderOutput | SemanticUsages.DomainShaderInput, IntrinsicTypes.Float2, IntrinsicTypes.Float3, IntrinsicTypes.Float4));
            allSemantics.Add(new SemanticSymbol("SV_VertexID", "Per-vertex identifier automatically generated by the runtime.", false, SemanticUsages.VertexShaderInput, IntrinsicTypes.Uint));
            allSemantics.Add(new SemanticSymbol("SV_ViewportArrayIndex", "Viewport array index. Applied to geometry shader output and indicates which viewport to use for the primitive currently being written out. The primitive will be transformed and clipped against the viewport specified by the index before it is passed to the rasterizer. This semantic applies only to primitives, if a primitive has more than one vertex the value from the leading vertex will be used.", false, SemanticUsages.GeometryShaderOutput | SemanticUsages.PixelShaderInput | SemanticUsages.PixelShaderOutput, IntrinsicTypes.Uint));

            // D3D9
            allSemantics.Add(new SemanticSymbol("BINORMAL", "Binormal.", true, SemanticUsages.VertexShaderInput));
            allSemantics.Add(new SemanticSymbol("BLENDINDICES", "Blend indices.", true, SemanticUsages.VertexShaderInput));
            allSemantics.Add(new SemanticSymbol("BLENDWEIGHT", "Blend weights.", true, SemanticUsages.VertexShaderInput));
            allSemantics.Add(new SemanticSymbol("COLOR", "Diffuse or specular color.", true, SemanticUsages.VertexShaderInput | SemanticUsages.VertexShaderOutput | SemanticUsages.PixelShaderInput | SemanticUsages.PixelShaderOutput));
            allSemantics.Add(new SemanticSymbol("NORMAL", "Normal vector.", true, SemanticUsages.VertexShaderInput));
            allSemantics.Add(new SemanticSymbol("POSITION", "Used as input: Vertex position in object space.\nUsed as output: Position of a vertex in homogenous space. Compute position in screen-space by dividing (x,y,z) by w. Every (D3D9) vertex shader must write out a parameter with this semantic.", true, SemanticUsages.VertexShaderInput));
            allSemantics.Add(new SemanticSymbol("POSITIONT", "Transformed vertex position.", false, SemanticUsages.VertexShaderInput));
            allSemantics.Add(new SemanticSymbol("PSIZE", "Point size.", true, SemanticUsages.VertexShaderInput));
            allSemantics.Add(new SemanticSymbol("TANGENT", "Tangent.", true, SemanticUsages.VertexShaderInput));
            allSemantics.Add(new SemanticSymbol("TEXCOORD", "Texture coordinates", true, SemanticUsages.VertexShaderInput | SemanticUsages.VertexShaderOutput | SemanticUsages.PixelShaderInput));
            allSemantics.Add(new SemanticSymbol("FOG", "Vertex fog.", false, SemanticUsages.VertexShaderOutput));
            allSemantics.Add(new SemanticSymbol("PSIZE", "Point size.", true, SemanticUsages.VertexShaderOutput | SemanticUsages.PixelShaderInput));
            allSemantics.Add(new SemanticSymbol("TESSFACTOR", "Tessellation factor.", true, SemanticUsages.VertexShaderOutput));
            allSemantics.Add(new SemanticSymbol("VFACE", "Floating-point scalar that indicates a back-facing primitive. A negative value faces backwards, while a positive value faces the camera.\nNote  This semantic is available in Direct3D 9 Shader Model 3.0. For Direct3D 10 and later, use SV_IsFrontFace instead.", false, SemanticUsages.PixelShaderInput));
            allSemantics.Add(new SemanticSymbol("VPOS", "The pixel location (x,y) in screen space.", false, SemanticUsages.PixelShaderInput));
            allSemantics.Add(new SemanticSymbol("DEPTH", "Depth.", true, SemanticUsages.PixelShaderOutput));

            AllSemantics = allSemantics.ToImmutableArray();
        }
Example #11
0
 public Agent(string assignment, Book book)
     : base("Agent", assignment, book)
 {
     var careers = new List<NormalCareer>
     {
         new Corporate(book),
         new Worker(book),
         new Colonist(book),
         new Thief(book),
         new Enforcer(book),
         new Pirate(book)
     };
     m_Careers = careers.ToImmutableArray();
 }
Example #12
0
		internal static async Task TestProvider(string file, string fileName,
			Func<Solution, ProjectId, Solution> modifySolution,
			Func<ImmutableArray<CodeAction>, Task> handleActions)
		{
			var code = File.ReadAllText(file);
			var document = TestHelpers.CreateDocument(code, fileName, modifySolution);
			var actions = new List<CodeAction>();
			var actionRegistration = new Action<CodeAction>(action => actions.Add(action));
			var context = new CodeRefactoringContext(document, new TextSpan(0, 1),
				actionRegistration, new CancellationToken(false));

			var provider = new ExtractTypesToFilesCodeRefactoringProvider();
			await provider.ComputeRefactoringsAsync(context);
			await handleActions(actions.ToImmutableArray());
		}
            public async Task<IInlineRenameLocationSet> FindRenameLocationsAsync(OptionSet optionSet, CancellationToken cancellationToken)
            {
                var references = new List<InlineRenameLocation>();

                var renameLocations = await _renameInfo.FindRenameLocationsAsync(
                    renameInStrings: optionSet.GetOption(RenameOptions.RenameInStrings),
                    renameInComments: optionSet.GetOption(RenameOptions.RenameInComments),
                    cancellationToken: cancellationToken).ConfigureAwait(false);

                references.AddRange(renameLocations.Select(
                    ds => new InlineRenameLocation(ds.Document, ds.TextSpan)));

                return new InlineRenameLocationSet(
                    _renameInfo, _document.Project.Solution,
                    references.ToImmutableArray());
            }
Example #14
0
        private static ImmutableArray<Cell> ProcessLine(string line, int x)
        {
            var cells = new List<Cell>();

            for (var y = 0; y < line.Length; y++)
            {
                var value = line[y];

                if (value != ' ')
                {
                    cells.Add(new Cell(new Point(x, y), value));
                }
            }

            return cells.ToImmutableArray();
        }
Example #15
0
        public ImmutableArray<AlaCarteAssignmentModel> GetUnsentEmails()
        {
            if (SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref operating, 1, 0) != 0, TimeSpan.FromSeconds(30)))
            {
                try
                {
                    bool alreadyOpen = Connection.State == ConnectionState.Open;
                    if (!alreadyOpen)
                        Connection.Open();
                    try
                    {
                        IList<Exception> exceptions = null;
                        IList<AlaCarteAssignmentModel> emails = new List<AlaCarteAssignmentModel>();
                        using (var command = CreateCommand(Connection))
                        using (var reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                try
                                {
                                    var email = Load(reader);
                                    emails.Add(email);
                                }
                                catch(Exception ex)
                                {
                                    (exceptions ?? (exceptions = new List<Exception>(1))).Add(ex);
                                }
                            }
                        }

                        return emails.ToImmutableArray();
                    }
                    finally
                    {
                        if (!alreadyOpen && Connection != null)
                            Connection.Close();
                    }
                }
                finally
                {
                    Interlocked.CompareExchange(ref operating, 0, 1);
                }
            }
            return ImmutableArray<AlaCarteAssignmentModel>.Empty;
        }
Example #16
0
        private static ImmutableArray<DiagnosticAnalyzer> GetAllAnalyzers()
        {
            Assembly assembly = typeof(StyleCop.Analyzers.NoCodeFixAttribute).Assembly;

            var diagnosticAnalyzerType = typeof(DiagnosticAnalyzer);

            List<DiagnosticAnalyzer> analyzers = new List<DiagnosticAnalyzer>();

            foreach (var type in assembly.GetTypes())
            {
                if (type.IsSubclassOf(diagnosticAnalyzerType) && !type.IsAbstract)
                {
                    analyzers.Add((DiagnosticAnalyzer)Activator.CreateInstance(type));
                }
            }

            return analyzers.ToImmutableArray();
        }
Example #17
0
 public static ImmutableArray<IMarkdownToken> ParseInlineToken(
     IMarkdownParser parser,
     IMarkdownRule rule,
     ImmutableArray<IMarkdownToken> blockTokens,
     bool wrapParagraph)
 {
     var result = new List<IMarkdownToken>(blockTokens.Length);
     var textContent = StringBuffer.Empty;
     foreach (var token in blockTokens)
     {
         var text = token as MarkdownTextToken;
         if (text != null)
         {
             if (textContent != StringBuffer.Empty)
             {
                 textContent += "\n";
             }
             textContent += text.Content;
             continue;
         }
         var newLine = token as MarkdownNewLineBlockToken;
         if (newLine?.RawMarkdown.Length == 1)
         {
             continue;
         }
         if (textContent != StringBuffer.Empty)
         {
             var rawMarkdown = textContent.ToString();
             result.Add(CreateToken(parser, rule, wrapParagraph, rawMarkdown));
             textContent = StringBuffer.Empty;
         }
         if (newLine != null)
         {
             continue;
         }
         result.Add(token);
     }
     if (textContent != StringBuffer.Empty)
     {
         var rawMarkdown = textContent.ToString();
         result.Add(CreateToken(parser, rule, wrapParagraph, rawMarkdown));
     }
     return result.ToImmutableArray();
 }
Example #18
0
        public ImmutableArray<Cell> Parse()
        {
            var cells = new List<Cell>();

            if (this.lines != null)
            {
                for (var i = 0; i < this.lines.Length; i++)
                {
                    var line = this.lines[i];

                    if (line != null)
                    {
                        cells.AddRange(Parser.ProcessLine(line, i));
                    }
                }
            }

            return cells.ToImmutableArray();
        }
Example #19
0
 public static ImmutableArray<IMarkdownToken> CreateParagraghs(
     IMarkdownParser parser,
     IMarkdownRule rule,
     ImmutableArray<IMarkdownToken> blockTokens,
     bool wrapParagraph,
     SourceInfo sourceInfo)
 {
     var result = new List<IMarkdownToken>(blockTokens.Length);
     var textContent = StringBuffer.Empty;
     var si = sourceInfo;
     foreach (var token in blockTokens)
     {
         var text = token as MarkdownTextToken;
         if (text != null)
         {
             if (textContent == StringBuffer.Empty)
             {
                 si = text.SourceInfo;
             }
             textContent += text.Content;
             continue;
         }
         if (textContent != StringBuffer.Empty)
         {
             result.Add(GroupTextTokens(parser, rule, wrapParagraph, textContent, si));
             textContent = StringBuffer.Empty;
         }
         if (token is MarkdownNewLineBlockToken)
         {
             continue;
         }
         result.Add(token);
     }
     if (textContent != StringBuffer.Empty)
     {
         result.Add(GroupTextTokens(parser, rule, wrapParagraph, textContent, si));
     }
     return result.ToImmutableArray();
 }
        // internal for testing
        internal static Args ParseArgs(string[] args)
        {
            string peFile              = null;
            bool   extract             = false;
            bool   sourceLink          = false;
            string inPdb               = null;
            string outPdb              = null;
            bool   suppressAllWarnings = false;
            var    suppressedWarnings  = new List <PdbDiagnosticId>();
            var    srcSvrVariables     = new List <KeyValuePair <string, string> >();

            int i = 0;

            while (i < args.Length)
            {
                var arg = args[i++];

                string ReadValue() => (i < args.Length) ? args[i++] : throw new InvalidDataException(string.Format(Resources.MissingValueForOption, arg));

                switch (arg)
                {
                case "/extract":
                    extract = true;
                    break;

                case "/sourcelink":
                    sourceLink = true;
                    break;

                case "/pdb":
                    inPdb = ReadValue();
                    break;

                case "/out":
                    outPdb = ReadValue();
                    break;

                case "/nowarn":
                    var value = ReadValue();
                    if (value == "*")
                    {
                        suppressAllWarnings = true;
                    }
                    else
                    {
                        suppressedWarnings.AddRange(value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(
                                                        n => (int.TryParse(n, out int id) && id != 0 && ((PdbDiagnosticId)id).IsValid()) ?
                                                        (PdbDiagnosticId)id :
                                                        throw new InvalidDataException(string.Format(Resources.InvalidWarningNumber, n))));
                    }

                    break;

                case "/srcsvrvar":
                case "/srcsvrvariable":
                    srcSvrVariables.Add(ParseSrcSvrVariable(ReadValue()));
                    break;

                default:
                    if (peFile == null)
                    {
                        peFile = arg;
                    }
                    else
                    {
                        throw new InvalidDataException((arg.StartsWith("/", StringComparison.Ordinal) ?
                                                        string.Format(Resources.UnrecognizedOption, arg) :
                                                        Resources.OnlyOneDllExeCanBeSpecified));
                    }
                    break;
                }
            }

            if (peFile == null)
            {
                throw new InvalidDataException(Resources.MissingDllExePath);
            }

            if (!extract && outPdb == null)
            {
                try
                {
                    outPdb = Path.ChangeExtension(peFile, "pdb2");
                }
                catch (Exception e)
                {
                    throw new InvalidDataException(e.Message);
                }
            }

            if (extract && sourceLink)
            {
                throw new InvalidDataException(Resources.CantSpecifyBothExtractAndSourcelinkOptions);
            }

            if (extract && inPdb != null)
            {
                throw new InvalidDataException(Resources.CantSpecifyBothExtractAndPdbOptions);
            }

            if (sourceLink && srcSvrVariables.Count > 0)
            {
                throw new InvalidDataException(Resources.CantSpecifyBothSrcSvrVariableAndSourcelinkOptions);
            }

            PortablePdbConversionOptions options;

            try
            {
                options = new PortablePdbConversionOptions(
                    suppressSourceLinkConversion: sourceLink,
                    srcSvrVariables: srcSvrVariables);
            }
            catch (ArgumentException e)
            {
                throw new InvalidDataException(e.Message);
            }

            return(new Args(peFile, inPdb, outPdb, options, suppressedWarnings.ToImmutableArray(), suppressAllWarnings, extract));
        }
        private ImmutableArray<DiagnosticData> UpdateDocumentDiagnostics(
            AnalysisData existingData, ImmutableArray<TextSpan> range, ImmutableArray<DiagnosticData> memberDiagnostics,
            SyntaxTree tree, SyntaxNode member, int memberId)
        {
            // get old span
            var oldSpan = range[memberId];

            // get old diagnostics
            var diagnostics = existingData.Items;

            // check quick exit cases
            if (diagnostics.Length == 0 && memberDiagnostics.Length == 0)
            {
                return diagnostics;
            }

            // simple case
            if (diagnostics.Length == 0 && memberDiagnostics.Length > 0)
            {
                return memberDiagnostics;
            }

            // regular case
            var result = new List<DiagnosticData>();

            // update member location
            Contract.Requires(member.FullSpan.Start == oldSpan.Start);
            var delta = member.FullSpan.End - oldSpan.End;

            var replaced = false;
            foreach (var diagnostic in diagnostics)
            {
                if (diagnostic.TextSpan.Start < oldSpan.Start)
                {
                    result.Add(diagnostic);
                    continue;
                }

                if (!replaced)
                {
                    result.AddRange(memberDiagnostics);
                    replaced = true;
                }

                if (oldSpan.End <= diagnostic.TextSpan.Start)
                {
                    result.Add(UpdatePosition(diagnostic, tree, delta));
                    continue;
                }
            }

            // if it haven't replaced, replace it now
            if (!replaced)
            {
                result.AddRange(memberDiagnostics);
                replaced = true;
            }

            return result.ToImmutableArray();
        }
        public async Task <CodeAnalysisResults> RunCodeAnalysisAsync(
            IEnumerable <UnsavedFile> unsavedFiles, Func <bool> interruptRequested)
        {
            var errorList   = IoC.Get <IErrorList>();
            var result      = new CodeAnalysisResults();
            var diagnostics = new List <Diagnostic>();

            var file            = _editor.SourceFile;
            var dataAssociation = GetAssociatedData(file);

            var currentUnsavedFile = unsavedFiles.FirstOrDefault(f => f.FileName == file.FilePath);
            var currentFileConts   = currentUnsavedFile?.Contents ?? File.ReadAllText(file.FilePath);
            var currentFileName    = currentUnsavedFile?.FileName ?? file.FilePath;
            TypeScriptSyntaxTree syntaxTree;
            // Only one analyzer at a time; the JS engine is single-threaded. TODO: Workaround with multiple JS engines
            await analysisThreadSemaphore.WaitAsync();

            try
            {
                syntaxTree = await _typeScriptContext.BuildAstAsync(currentFileName, currentFileConts);
            }
            catch (JavaScriptException)
            {
                diagnostics.Add(new Diagnostic(
                                    0, 0,
                                    _editor.SourceFile.Project.Name,
                                    _editor.SourceFile.Location,
                                    0,
                                    "Code analysis language service call failed.",
                                    "INT001",
                                    DiagnosticLevel.Error,
                                    DiagnosticCategory.Compiler));

                errorList.Remove((this, _editor.SourceFile));
                errorList.Create((this, _editor.SourceFile), null, DiagnosticSourceKind.Analysis, diagnostics.ToImmutableArray());

                return(new CodeAnalysisResults());
            }
            finally
            {
                analysisThreadSemaphore.Release();
            }

#if DEBUG
            var syntaxTreeJsonDebug = JsonConvert.SerializeObject(syntaxTree);
#endif

            var lineCommentMatches = LineCommentPattern.Matches(currentFileConts);
            foreach (Match commentMatch in lineCommentMatches)
            {
                result.SyntaxHighlightingData.Add(new OffsetSyntaxHighlightingData
                {
                    Start  = commentMatch.Index,
                    Length = commentMatch.Length,
                    Type   = HighlightType.Comment
                });
            }

            var blockCommentMatches = BlockCommentPattern.Matches(currentFileConts);
            foreach (Match commentMatch in blockCommentMatches)
            {
                result.SyntaxHighlightingData.Add(new OffsetSyntaxHighlightingData
                {
                    Start  = commentMatch.Index,
                    Length = commentMatch.Length,
                    Type   = HighlightType.Comment
                });
            }

            // Highlight keywords
            var keywordMatches = KeywordPattern.Matches(currentFileConts);
            foreach (Match keywordMatch in keywordMatches)
            {
                result.SyntaxHighlightingData.Add(new OffsetSyntaxHighlightingData
                {
                    Start  = keywordMatch.Index,
                    Length = keywordMatch.Length,
                    Type   = HighlightType.Keyword
                });
            }

            // Recursively highlight and analyze from parse tree
            foreach (var rootStatement in syntaxTree.Statements)
            {
                HighlightNode(rootStatement, result);
            }

            // Diagnostics
            foreach (var diagnostic in syntaxTree.ParseDiagnostics)
            {
                // Convert diagnostics
                diagnostics.Add(new Diagnostic(
                                    diagnostic.Start,
                                    diagnostic.Length,
                                    _editor.SourceFile.Project.Name,
                                    _editor.SourceFile.Location,
                                    GetLineNumber(currentFileConts, diagnostic.Start),
                                    diagnostic.MessageText,
                                    "INT002",
                                    diagnostic.Category == TSBridge.Ast.Diagnostics.Diagnostic.DiagnosticCategory.Error
                        ? DiagnosticLevel.Error
                        : DiagnosticLevel.Warning,
                                    DiagnosticCategory.Compiler
                                    ));
            }

            diagnostics.Add(new Diagnostic(
                                0,
                                0,
                                _editor.SourceFile.Project.Name,
                                _editor.SourceFile.Location,
                                0,
                                "Code analysis for TypeScript is experimental and unstable. Use with caution.",
                                "INT003",
                                DiagnosticLevel.Warning,
                                DiagnosticCategory.Compiler));

            errorList.Remove((this, _editor.SourceFile));
            errorList.Create((this, _editor.SourceFile), null, DiagnosticSourceKind.Analysis, diagnostics.ToImmutableArray());

            return(result);
        }
            public AnalysisResult ToResult()
            {
                var documentIds    = _lazySet == null ? ImmutableHashSet <DocumentId> .Empty : _lazySet.ToImmutableHashSet();
                var syntaxLocals   = Convert(_lazySyntaxLocals);
                var semanticLocals = Convert(_lazySemanticLocals);
                var nonLocals      = Convert(_lazyNonLocals);
                var others         = _lazyOthers == null ? ImmutableArray <DiagnosticData> .Empty : _lazyOthers.ToImmutableArray();

                return(new AnalysisResult(_project.Id, _version, syntaxLocals, semanticLocals, nonLocals, others, documentIds, fromBuild: false));
            }
        private ImmutableArray <DiagnosticData> UpdateDocumentDiagnostics(
            AnalysisData existingData, ImmutableArray <TextSpan> range, ImmutableArray <DiagnosticData> memberDiagnostics,
            SyntaxTree tree, SyntaxNode member, int memberId)
        {
            // get old span
            var oldSpan = range[memberId];

            // get old diagnostics
            var diagnostics = existingData.Items;

            // check quick exit cases
            if (diagnostics.Length == 0 && memberDiagnostics.Length == 0)
            {
                return(diagnostics);
            }

            // simple case
            if (diagnostics.Length == 0 && memberDiagnostics.Length > 0)
            {
                return(memberDiagnostics);
            }

            // regular case
            var result = new List <DiagnosticData>();

            // update member location
            Contract.Requires(member.FullSpan.Start == oldSpan.Start);
            var delta = member.FullSpan.End - oldSpan.End;

            var replaced = false;

            foreach (var diagnostic in diagnostics)
            {
                if (diagnostic.TextSpan.Start < oldSpan.Start)
                {
                    result.Add(diagnostic);
                    continue;
                }

                if (!replaced)
                {
                    result.AddRange(memberDiagnostics);
                    replaced = true;
                }

                if (oldSpan.End <= diagnostic.TextSpan.Start)
                {
                    result.Add(UpdatePosition(diagnostic, tree, delta));
                    continue;
                }
            }

            // if it haven't replaced, replace it now
            if (!replaced)
            {
                result.AddRange(memberDiagnostics);
                replaced = true;
            }

            return(result.ToImmutableArray());
        }
Example #25
0
        public static Expression CreateParenthesized(Context cx, VarPatternSyntax varPattern, ParenthesizedVariableDesignationSyntax designation, IExpressionParentEntity parent, int child)
        {
            var tuple = new Expression(
                new ExpressionInfo(cx, null, cx.CreateLocation(varPattern.GetLocation()), ExprKind.TUPLE, parent, child, false, null),
                shouldPopulate: false);

            var elementTypes = new List <ITypeSymbol?>();

            cx.Try(null, null, () =>
            {
                var child0 = 0;
                foreach (var variable in designation.Variables)
                {
                    Expression sub;
                    switch (variable)
                    {
                    case ParenthesizedVariableDesignationSyntax paren:
                        sub = CreateParenthesized(cx, varPattern, paren, tuple, child0++);
                        break;

                    case SingleVariableDesignationSyntax single:
                        if (cx.GetModel(variable).GetDeclaredSymbol(single) is ILocalSymbol local)
                        {
                            var decl = Create(cx, variable, local.GetAnnotatedType(), tuple, child0++);
                            var l    = LocalVariable.Create(cx, local);
                            l.PopulateManual(decl, true);
                            sub = decl;
                        }
                        else
                        {
                            throw new InternalError(single, "Failed to access local variable");
                        }
                        break;

                    case DiscardDesignationSyntax discard:
                        sub = new Discard(cx, discard, tuple, child0++);
                        if (!sub.Type.HasValue || sub.Type.Value.Symbol is null)
                        {
                            // The type is only updated in memory, it will not be written to the trap file.
                            sub.SetType(cx.Compilation.GetSpecialType(SpecialType.System_Object));
                        }
                        break;

                    default:
                        var type = variable.GetType().ToString() ?? "null";
                        throw new InternalError(variable, $"Unhandled designation type {type}");
                    }

                    elementTypes.Add(sub.Type.HasValue && sub.Type.Value.Symbol?.Kind != SymbolKind.ErrorType
                        ? sub.Type.Value.Symbol
                        : null);
                }
            });

            INamedTypeSymbol?tupleType = null;

            if (!elementTypes.Any(et => et is null))
            {
                tupleType = cx.Compilation.CreateTupleTypeSymbol(elementTypes.ToImmutableArray() !);
            }

            tuple.SetType(tupleType);
            tuple.TryPopulate();

            return(tuple);
        }
Example #26
0
        internal BatchSignInput GenerateListOfFiles()
        {
            foreach (var fullPath in _explicitSignList)
            {
                TrackFile(fullPath, ContentUtil.GetContentHash(fullPath), isNested: false);
            }

            return(new BatchSignInput(_filesToSign.ToImmutableArray(), _zipDataMap.ToImmutableDictionary(ByteSequenceComparer.Instance), _filesToCopy.ToImmutableArray()));
        }
        /// <summary>
        /// Resolves metadata references stored in command line arguments and reports errors for those that can't be resolved.
        /// </summary>
        internal List<MetadataReference> ResolveMetadataReferences(
            List<DiagnosticInfo> diagnostics,
            TouchedFileLogger touchedFiles,
            out MetadataReferenceResolver referenceDirectiveResolver)
        {
            var commandLineReferenceResolver = GetCommandLineMetadataReferenceResolver(touchedFiles);

            List<MetadataReference> resolved = new List<MetadataReference>();
            Arguments.ResolveMetadataReferences(commandLineReferenceResolver, diagnostics, this.MessageProvider, resolved);

            if (Arguments.IsScriptRunner)
            {
                referenceDirectiveResolver = commandLineReferenceResolver;
            }
            else
            {
                // when compiling into an assembly (csc/vbc) we only allow #r that match references given on command line:
                referenceDirectiveResolver = new ExistingReferencesResolver(commandLineReferenceResolver, resolved.ToImmutableArray());
            }

            return resolved;
        }
Example #28
0
        public static ImmutableArray <SymbolDisplayPart> ToDisplayParts(this ISymbol symbol, SymbolDisplayFormat format, SymbolDisplayAdditionalMemberOptions additionalOptions)
        {
            if (additionalOptions == SymbolDisplayAdditionalMemberOptions.None)
            {
                return(symbol.ToDisplayParts(format));
            }

            ImmutableArray <SymbolDisplayPart> parts = symbol.ToDisplayParts(format);
            int length = parts.Length;

            for (int i = 0; i < length; i++)
            {
                SymbolDisplayPart part = parts[i];

                switch (part.Kind)
                {
                case SymbolDisplayPartKind.Keyword:
                {
                    switch (part.ToString())
                    {
                    case "this":
                    {
                        if ((additionalOptions & SymbolDisplayAdditionalMemberOptions.UseItemPropertyName) != 0 &&
                            (symbol as IPropertySymbol)?.IsIndexer == true)
                        {
                            parts = parts.Replace(part, SymbolDisplayPartFactory.PropertyName("Item", part.Symbol));
                        }

                        break;
                    }

                    case "operator":
                    {
                        if ((additionalOptions & SymbolDisplayAdditionalMemberOptions.UseOperatorName) != 0 &&
                            symbol is IMethodSymbol methodSymbol &&
                            methodSymbol.MethodKind == MethodKind.UserDefinedOperator)
                        {
                            string name = methodSymbol.Name;

                            Debug.Assert(name.StartsWith("op_", StringComparison.Ordinal), name);

                            if (name.StartsWith("op_", StringComparison.Ordinal) &&
                                i < length - 2 &&
                                parts[i + 1].IsSpace() &&
                                parts[i + 2].Kind == SymbolDisplayPartKind.MethodName)
                            {
                                parts   = parts.Replace(parts[i + 2], SymbolDisplayPartFactory.MethodName(name.Substring(3), parts[i + 2].Symbol));
                                parts   = parts.RemoveRange(i, 2);
                                length -= 2;
                            }
                        }

                        break;
                    }

                    case "implicit":
                    case "explicit":
                    {
                        if ((additionalOptions & SymbolDisplayAdditionalMemberOptions.UseOperatorName) != 0 &&
                            symbol is IMethodSymbol methodSymbol &&
                            methodSymbol.MethodKind == MethodKind.Conversion)
                        {
                            string name = methodSymbol.Name;

                            Debug.Assert(name.StartsWith("op_", StringComparison.Ordinal), name);

                            if (name.StartsWith("op_", StringComparison.Ordinal) &&
                                i < length - 2 &&
                                parts[i + 1].IsSpace() &&
                                parts[i + 2].IsKeyword("operator"))
                            {
                                List <SymbolDisplayPart> list = parts.ToList();

                                list[i + 2] = SymbolDisplayPartFactory.MethodName(name.Substring(3), list[i + 4].Symbol);
                                list.RemoveRange(i, 2);
                                length -= 2;

                                if (i == length - 3 &&
                                    list[i + 1].IsSpace() &&
                                    list[i + 2].IsName())
                                {
                                    list.RemoveRange(i + 1, 2);
                                    length -= 2;
                                }
                                else if (i < length - 5 &&
                                         list[i + 1].IsSpace() &&
                                         list[i + 2].IsName() &&
                                         list[i + 3].IsPunctuation() &&
                                         list[i + 4].IsName() &&
                                         list[i + 5].IsPunctuation())
                                {
                                    list.Insert(i + 5, list[i + 2]);
                                    list.Insert(i + 5, SymbolDisplayPartFactory.Text(" to "));
                                    list.RemoveRange(i + 1, 2);
                                    length -= 5;
                                }

                                parts = list.ToImmutableArray();
                            }
                        }

                        break;
                    }
                    }

                    break;
                }
                }
            }

            return(parts);
        }
Example #29
0
        /// <summary>
        /// Build up the <see cref="ZipData"/> instance for a given zip container. This will also report any consistency
        /// errors found when examining the zip archive.
        /// </summary>
        private bool TryBuildZipData(FileSignInfo zipFileSignInfo, out ZipData zipData)
        {
            Debug.Assert(zipFileSignInfo.IsZipContainer());

            try
            {
                using (var archive = new ZipArchive(File.OpenRead(zipFileSignInfo.FullPath), ZipArchiveMode.Read))
                {
                    var nestedParts = new List <ZipPart>();

                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        string relativePath = entry.FullName;
                        string extension    = Path.GetExtension(relativePath);

                        if (!_fileExtensionSignInfo.TryGetValue(extension, out var extensionSignInfo) || !extensionSignInfo.ShouldSign)
                        {
                            continue;
                        }

                        ImmutableArray <byte> contentHash;
                        using (var stream = entry.Open())
                        {
                            contentHash = ContentUtil.GetContentHash(stream);
                        }

                        // if we already encountered file that hash the same content we can reuse its signed version when repackaging the container.
                        string fileName = Path.GetFileName(relativePath);
                        if (!_filesByContentKey.TryGetValue(new SignedFileContentKey(contentHash, fileName), out var fileSignInfo))
                        {
                            string tempDir  = Path.Combine(_pathToContainerUnpackingDirectory, ContentUtil.HashToString(contentHash));
                            string tempPath = Path.Combine(tempDir, Path.GetFileName(relativePath));
                            Directory.CreateDirectory(tempDir);

                            using (var stream = entry.Open())
                                using (var tempFileStream = File.OpenWrite(tempPath))
                                {
                                    stream.CopyTo(tempFileStream);
                                }

                            fileSignInfo = TrackFile(tempPath, contentHash, isNested: true);
                        }

                        if (fileSignInfo.SignInfo.ShouldSign)
                        {
                            nestedParts.Add(new ZipPart(relativePath, fileSignInfo));
                        }
                    }

                    zipData = new ZipData(zipFileSignInfo, nestedParts.ToImmutableArray());

                    return(true);
                }
            }
            catch (Exception e)
            {
                _log.LogErrorFromException(e);
                zipData = null;
                return(false);
            }
        }
Example #30
0
        public SemanticModel(Compilation compilation, BicepFile sourceFile, IFileResolver fileResolver, IBicepAnalyzer linterAnalyzer)
        {
            Trace.WriteLine($"Building semantic model for {sourceFile.FileUri}");

            Compilation  = compilation;
            SourceFile   = sourceFile;
            FileResolver = fileResolver;

            // create this in locked mode by default
            // this blocks accidental type or binding queries until binding is done
            // (if a type check is done too early, unbound symbol references would cause incorrect type check results)
            var symbolContext = new SymbolContext(compilation, this);

            SymbolContext = symbolContext;

            Binder      = new Binder(compilation.NamespaceProvider, sourceFile, symbolContext);
            TypeManager = new TypeManager(Binder, fileResolver);

            // name binding is done
            // allow type queries now
            symbolContext.Unlock();

            this.emitLimitationInfoLazy = new Lazy <EmitLimitationInfo>(() => EmitLimitationCalculator.Calculate(this));
            this.symbolHierarchyLazy    = new Lazy <SymbolHierarchy>(() =>
            {
                var hierarchy = new SymbolHierarchy();
                hierarchy.AddRoot(this.Root);

                return(hierarchy);
            });
            this.resourceAncestorsLazy = new Lazy <ResourceAncestorGraph>(() => ResourceAncestorGraph.Compute(this));
            this.ResourceMetadata      = new ResourceMetadataCache(this);

            LinterAnalyzer = linterAnalyzer;

            this.allResourcesLazy = new Lazy <ImmutableArray <ResourceMetadata> >(() => GetAllResourceMetadata());

            // lazy load single use diagnostic set
            this.allDiagnostics = new Lazy <IEnumerable <IDiagnostic> >(() => AssembleDiagnostics());

            this.parameterTypePropertiesLazy = new Lazy <ImmutableArray <TypeProperty> >(() =>
            {
                var paramTypeProperties = new List <TypeProperty>();

                foreach (var param in this.Root.ParameterDeclarations.DistinctBy(p => p.Name))
                {
                    var typePropertyFlags = TypePropertyFlags.WriteOnly;
                    if (SyntaxHelper.TryGetDefaultValue(param.DeclaringParameter) == null)
                    {
                        // if there's no default value, it must be specified
                        typePropertyFlags |= TypePropertyFlags.Required;
                    }

                    var description = SemanticModelHelper.TryGetDescription(this, param.DeclaringParameter);
                    paramTypeProperties.Add(new TypeProperty(param.Name, param.Type, typePropertyFlags, description));
                }

                return(paramTypeProperties.ToImmutableArray());
            });

            this.outputTypePropertiesLazy = new Lazy <ImmutableArray <TypeProperty> >(() =>
            {
                var outputTypeProperties = new List <TypeProperty>();

                foreach (var output in this.Root.OutputDeclarations.DistinctBy(o => o.Name))
                {
                    var description = SemanticModelHelper.TryGetDescription(this, output.DeclaringOutput);
                    outputTypeProperties.Add(new TypeProperty(output.Name, output.Type, TypePropertyFlags.ReadOnly, description));
                }

                return(outputTypeProperties.ToImmutableArray());
            });
        }
Example #31
0
        public static ImmutableArray <RawToken> Tokens(string sql)
        {
            if (string.IsNullOrEmpty(sql))
            {
                return(ImmutableArray <RawToken> .Empty);
            }

            var tokens   = new List <RawToken>();
            var position = 0;

            while (position < sql.Length)
            {
                TokenParser.SkipWhitespace(sql, ref position);
                if (TokenParser.TryParseNumber(sql, position, out var token) ||
                    TokenParser.TryParseString(sql, position, out token) ||
                    TokenParser.TryParseComment(sql, position, out token) ||
                    TokenParser.TryParseIdentifier(sql, position, out token) ||
                    TokenParser.TryParse(sql, position, "<>", SqlKind.NotEqualsToken, out token) ||
                    TokenParser.TryParse(sql, position, "<=", SqlKind.LessThanEqualsToken, out token) ||
                    TokenParser.TryParse(sql, position, ">=", SqlKind.GreaterThanEqualsToken, out token) ||
                    TokenParser.TryParse(sql, position, "|/", SqlKind.SquareRootToken, out token) ||
                    TokenParser.TryParse(sql, position, "||/", SqlKind.CubeRootToken, out token) ||
                    TokenParser.TryParse(sql, position, "!!", SqlKind.ExclamationExclamationToken, out token) ||
                    TokenParser.TryParse(sql, position, "<<", SqlKind.LessThanLessThanToken, out token) ||
                    TokenParser.TryParse(sql, position, ">>", SqlKind.GreaterThanGreaterThanToken, out token) ||
                    TokenParser.TryParse(sql, position, '+', SqlKind.PlusToken, out token) ||
                    TokenParser.TryParse(sql, position, '-', SqlKind.MinusToken, out token) ||
                    TokenParser.TryParse(sql, position, '*', SqlKind.AsteriskToken, out token) ||
                    TokenParser.TryParse(sql, position, '/', SqlKind.SlashToken, out token) ||
                    TokenParser.TryParse(sql, position, '^', SqlKind.ExponentToken, out token) ||
                    TokenParser.TryParse(sql, position, '%', SqlKind.PercentToken, out token) ||
                    TokenParser.TryParse(sql, position, '@', SqlKind.AtToken, out token) ||
                    TokenParser.TryParse(sql, position, '!', SqlKind.ExclamationToken, out token) ||
                    TokenParser.TryParse(sql, position, '&', SqlKind.AmpersandToken, out token) ||
                    TokenParser.TryParse(sql, position, '|', SqlKind.BarToken, out token) ||
                    TokenParser.TryParse(sql, position, '#', SqlKind.HashToken, out token) ||
                    TokenParser.TryParse(sql, position, '~', SqlKind.TildeToken, out token) ||
                    TokenParser.TryParse(sql, position, '=', SqlKind.EqualsToken, out token) ||
                    TokenParser.TryParse(sql, position, '<', SqlKind.LessThanEqualsToken, out token) ||
                    TokenParser.TryParse(sql, position, '>', SqlKind.GreaterThanToken, out token) ||
                    TokenParser.TryParse(sql, position, '(', SqlKind.OpenParenToken, out token) ||
                    TokenParser.TryParse(sql, position, ')', SqlKind.CloseParenToken, out token) ||
                    TokenParser.TryParse(sql, position, '[', SqlKind.OpenBracketToken, out token) ||
                    TokenParser.TryParse(sql, position, ']', SqlKind.CloseBracketToken, out token) ||
                    TokenParser.TryParse(sql, position, '.', SqlKind.DotToken, out token) ||
                    TokenParser.TryParse(sql, position, ',', SqlKind.CommaToken, out token) ||
                    TokenParser.TryParse(sql, position, ';', SqlKind.SemicolonToken, out token) ||
                    TokenParser.TryParse(sql, position, ':', SqlKind.ColonToken, out token))
                {
                    position = token.End;
                    tokens.Add(token);
                }
                else
                {
                    tokens.Add(RawToken.SingleChar(SqlKind.Unknown, position));
                    position++;
                }
            }

            return(tokens.ToImmutableArray());
        }
Example #32
0
        public virtual JassCompilationUnitSyntax Build(Map map)
        {
            if (map is null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            JassCommentDeclarationSyntax commentLine1 = new("===========================================================================");
            JassCommentDeclarationSyntax commentLine2 = new("***************************************************************************");
            JassCommentDeclarationSyntax commentLine3 = new("*");

            List <IDeclarationSyntax> declarations = new();
            var globalDeclarationList = new List <IDeclarationSyntax>();
            var generatedGlobals      = new List <JassGlobalDeclarationSyntax>();

            void AppendBanner(string bannerText)
            {
                declarations.Add(commentLine2);
                declarations.Add(commentLine3);
                declarations.Add(new JassCommentDeclarationSyntax($"*  {bannerText}"));
                declarations.Add(commentLine3);
                declarations.Add(commentLine2);
                declarations.Add(JassEmptyDeclarationSyntax.Value);
            }

            void AppendBannerAndFunction(string bannerText, Func <Map, JassFunctionDeclarationSyntax> function, Func <Map, bool> condition)
            {
                if (condition(map))
                {
                    AppendBanner(bannerText);
                    declarations.Add(function(map));
                    declarations.Add(JassEmptyDeclarationSyntax.Value);
                }
            }

            void AppendBannerAndFunctions(string bannerText, Func <Map, IEnumerable <IDeclarationSyntax> > functions, Func <Map, bool> condition)
            {
                if (condition(map))
                {
                    AppendBanner(bannerText);
                    foreach (var function in functions(map))
                    {
                        declarations.Add(function);
                        declarations.Add(JassEmptyDeclarationSyntax.Value);
                    }
                }
            }

            void AppendFunction(Func <Map, JassFunctionDeclarationSyntax> function, Func <Map, bool> condition)
            {
                if (condition(map))
                {
                    declarations.Add(commentLine1);
                    declarations.Add(function(map));
                    declarations.Add(JassEmptyDeclarationSyntax.Value);
                }
            }

            void AppendFunctionForIndex(int index, Func <Map, int, JassFunctionDeclarationSyntax> function, Func <Map, int, bool> condition)
            {
                if (condition(map, index))
                {
                    declarations.Add(commentLine1);
                    declarations.Add(function(map, index));
                    declarations.Add(JassEmptyDeclarationSyntax.Value);
                }
            }

            declarations.AddRange(GetMapScriptHeader(map));
            declarations.Add(JassEmptyDeclarationSyntax.Value);

            AppendBanner("Global Variables");
            generatedGlobals.AddRange(Regions(map));
            generatedGlobals.AddRange(Cameras(map));
            generatedGlobals.AddRange(Sounds(map));
            generatedGlobals.AddRange(Units(map));
            generatedGlobals.AddRange(RandomUnitTables(map));

            if (generatedGlobals.Any())
            {
                globalDeclarationList.Add(new JassCommentDeclarationSyntax(" Generated"));
                globalDeclarationList.AddRange(generatedGlobals);
            }

            declarations.Add(new JassGlobalDeclarationListSyntax(globalDeclarationList.ToImmutableArray()));
            declarations.Add(JassEmptyDeclarationSyntax.Value);

            AppendBanner("Custom Script Code");
            AppendBannerAndFunction("Random Groups", InitRandomGroups, InitRandomGroupsCondition);
            AppendBannerAndFunctions("Map Item Tables", MapItemTables, MapItemTablesCondition);
            AppendBannerAndFunction("Items", CreateAllItems, CreateAllItemsCondition);
            AppendBannerAndFunctions("Unit Item Tables", UnitItemTables, UnitItemTablesCondition);
            AppendBannerAndFunctions("Destructable Item Tables", DestructableItemTables, DestructableItemTablesCondition);
            AppendBannerAndFunction("Sounds", InitSounds, InitSoundsCondition);
            AppendBannerAndFunction("Destructable Objects", CreateAllDestructables, CreateAllDestructablesCondition);

            if (CreateAllUnitsCondition(map))
            {
                AppendBanner("Unit Creation");

                foreach (var i in Enumerable.Range(0, MaxPlayerSlots))
                {
                    AppendFunctionForIndex(i, CreateBuildingsForPlayer, CreateBuildingsForPlayerCondition);
                    AppendFunctionForIndex(i, CreateUnitsForPlayer, CreateUnitsForPlayerCondition);
                }

                AppendFunction(CreateNeutralHostile, CreateNeutralHostileCondition);
                AppendFunction(CreateNeutralPassiveBuildings, CreateNeutralPassiveBuildingsCondition);
                AppendFunction(CreateNeutralPassive, CreateNeutralPassiveCondition);
                AppendFunction(CreatePlayerBuildings, CreatePlayerBuildingsCondition);
                AppendFunction(CreatePlayerUnits, CreatePlayerUnitsCondition);
                AppendFunction(CreateNeutralUnits, CreateNeutralUnitsCondition);
                AppendFunction(CreateAllUnits, CreateAllUnitsCondition);
            }

            AppendBannerAndFunction("Regions", CreateRegions, CreateRegionsCondition);
            AppendBannerAndFunction("Cameras", CreateCameras, CreateCamerasCondition);

            AppendBanner("Players");

            declarations.Add(InitCustomPlayerSlots(map));
            declarations.Add(JassEmptyDeclarationSyntax.Value);

            declarations.Add(InitCustomTeams(map));
            declarations.Add(JassEmptyDeclarationSyntax.Value);

            var ids = Enumerable.Range(0, MaxPlayerSlots).ToArray();

            if (map.Info.Players.Any(p => ids.Any(id => p.AllyLowPriorityFlags[id] || p.AllyHighPriorityFlags[id])))
            {
                declarations.Add(InitAllyPriorities(map));
                declarations.Add(JassEmptyDeclarationSyntax.Value);
            }

            AppendBannerAndFunction("Main Initialization", main, mainCondition);
            AppendBannerAndFunction("Map Configuration", config, configCondition);

            return(SyntaxFactory.CompilationUnit(declarations));
        }
Example #33
0
        public static async Task<ImmutableArray<ISymbol>> GetSymbolsAsync(CompletionItem item, Document document, CancellationToken cancellationToken)
        {
            string symbolIds;
            if (item.Properties.TryGetValue("Symbols", out symbolIds))
            {
                var idList = symbolIds.Split(s_symbolSplitters, StringSplitOptions.RemoveEmptyEntries).ToList();
                var symbols = new List<ISymbol>();

                var compilation = await document.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
                DecodeSymbols(idList, compilation, symbols);

                // merge in symbols from other linked documents
                if (idList.Count > 0)
                {
                    var linkedIds = document.GetLinkedDocumentIds();
                    if (linkedIds.Length > 0)
                    {
                        foreach (var id in linkedIds)
                        {
                            var linkedDoc = document.Project.Solution.GetDocument(id);
                            var linkedCompilation = await linkedDoc.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
                            DecodeSymbols(idList, linkedCompilation, symbols);
                        }
                    }
                }

                return symbols.ToImmutableArray();
            }

            return ImmutableArray<ISymbol>.Empty;
        }
Example #34
0
 public LexResult Lex()
 {
     return(new LexResult(_source, LexTokens().ToImmutableArray(), _allDiagnostics.ToImmutableArray()));
 }
Example #35
0
        private VsixData VerifyVsixContentsBeforeSign(BinaryName vsixName, Dictionary<string, BinaryName> checksumToNameMap, ref bool allGood)
        {
            var nestedExternalBinaries = new List<string>();
            var nestedParts = new List<VsixPart>();
            using (var package = Package.Open(vsixName.FullPath, FileMode.Open, FileAccess.Read))
            {
                foreach (var part in package.GetParts())
                {
                    var relativeName = GetPartRelativeFileName(part);
                    var name = Path.GetFileName(relativeName);
                    if (!IsVsix(name) && !IsAssembly(name))
                    {
                        continue;
                    }

                    if (_signData.ExternalBinaryNames.Contains(name))
                    {
                        nestedExternalBinaries.Add(name);
                        continue;
                    }

                    if (!_signData.BinaryNames.Any(x => FilePathComparer.Equals(x.Name, name)))
                    {
                        allGood = false;
                        Console.WriteLine($"VSIX {vsixName} has part {name} which is not listed in the sign or external list");
                        continue;
                    }

                    // This represents a binary that we need to sign.  Ensure the content in the VSIX is the same as the 
                    // content in the binaries directory by doing a chekcsum match.
                    using (var stream = part.GetStream())
                    {
                        string checksum = _contentUtil.GetChecksum(stream);
                        BinaryName checksumName;
                        if (!checksumToNameMap.TryGetValue(checksum, out checksumName))
                        {
                            allGood = false;
                            Console.WriteLine($"{vsixName} has part {name} which does not match the content in the binaries directory");
                            continue;
                        }

                        if (!FilePathComparer.Equals(checksumName.Name, name))
                        {
                            allGood = false;
                            Console.WriteLine($"{vsixName} has part {name} with a different name in the binaries directory: {checksumName}");
                            continue;
                        }

                        nestedParts.Add(new VsixPart(relativeName, checksumName));
                    }
                }
            }

            return new VsixData(vsixName, nestedParts.ToImmutableArray(), nestedExternalBinaries.ToImmutableArray());
        }
Example #36
0
        internal static async Task <IntellisenseQuickInfoItem> BuildItemAsync(ITrackingSpan trackingSpan,
                                                                              CodeAnalysisQuickInfoItem quickInfoItem,
                                                                              ITextSnapshot snapshot,
                                                                              Document document,
                                                                              CancellationToken cancellationToken)
        {
            // Build the first line of QuickInfo item, the images and the Description section should be on the first line with Wrapped style
            var glyphs            = quickInfoItem.Tags.GetGlyphs();
            var symbolGlyph       = glyphs.FirstOrDefault(g => g != Glyph.CompletionWarning);
            var warningGlyph      = glyphs.FirstOrDefault(g => g == Glyph.CompletionWarning);
            var firstLineElements = new List <object>();

            if (symbolGlyph != Glyph.None)
            {
                firstLineElements.Add(new ImageElement(symbolGlyph.GetImageId()));
            }

            if (warningGlyph != Glyph.None)
            {
                firstLineElements.Add(new ImageElement(warningGlyph.GetImageId()));
            }

            var elements    = new List <object>();
            var descSection = quickInfoItem.Sections.FirstOrDefault(s => s.Kind == QuickInfoSectionKinds.Description);

            if (descSection != null)
            {
                var isFirstElement = true;
                foreach (var element in Helpers.BuildClassifiedTextElements(descSection))
                {
                    if (isFirstElement)
                    {
                        isFirstElement = false;
                        firstLineElements.Add(element);
                    }
                    else
                    {
                        // If the description section contains multiple paragraphs, the second and additional paragraphs
                        // are not wrapped in firstLineElements (they are normal paragraphs).
                        elements.Add(element);
                    }
                }
            }

            elements.Insert(0, new ContainerElement(ContainerElementStyle.Wrapped, firstLineElements));

            var documentationCommentSection = quickInfoItem.Sections.FirstOrDefault(s => s.Kind == QuickInfoSectionKinds.DocumentationComments);

            if (documentationCommentSection != null)
            {
                var isFirstElement = true;
                foreach (var element in Helpers.BuildClassifiedTextElements(documentationCommentSection))
                {
                    if (isFirstElement)
                    {
                        isFirstElement = false;

                        // Stack the first paragraph of the documentation comments with the last line of the description
                        // to avoid vertical padding between the two.
                        var lastElement = elements[elements.Count - 1];
                        elements[elements.Count - 1] = new ContainerElement(
                            ContainerElementStyle.Stacked,
                            lastElement,
                            element);
                    }
                    else
                    {
                        elements.Add(element);
                    }
                }
            }

            // Add the remaining sections as Stacked style
            elements.AddRange(
                quickInfoItem.Sections.Where(s => s.Kind != QuickInfoSectionKinds.Description && s.Kind != QuickInfoSectionKinds.DocumentationComments)
                .SelectMany(Helpers.BuildClassifiedTextElements));

            // build text for RelatedSpan
            if (quickInfoItem.RelatedSpans.Any())
            {
                var classifiedSpanList = new List <ClassifiedSpan>();
                foreach (var span in quickInfoItem.RelatedSpans)
                {
                    var classifiedSpans = await EditorClassifier.GetClassifiedSpansAsync(document, span, cancellationToken).ConfigureAwait(false);

                    classifiedSpanList.AddRange(classifiedSpans);
                }

                var tabSize = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.TabSize, document.Project.Language);
                var text    = await document.GetTextAsync().ConfigureAwait(false);

                var spans    = IndentationHelper.GetSpansWithAlignedIndentation(text, classifiedSpanList.ToImmutableArray(), tabSize);
                var textRuns = spans.Select(s => new ClassifiedTextRun(s.ClassificationType, snapshot.GetText(s.TextSpan.ToSpan())));

                if (textRuns.Any())
                {
                    elements.Add(new ClassifiedTextElement(textRuns));
                }
            }

            var content = new ContainerElement(
                ContainerElementStyle.Stacked | ContainerElementStyle.VerticalPadding,
                elements);

            return(new IntellisenseQuickInfoItem(trackingSpan, content));
        }
Example #37
0
        static IntrinsicAttributes()
        {
            var allAttributes = new List<AttributeSymbol>();

            allAttributes.Add(Create("allow_uav_condition", "Allows a compute shader loop termination condition to be based off of a UAV read. The loop must not contain synchronization intrinsics."));
            allAttributes.Add(Create("branch", "When applied to if statements: Evaluate only one side of the if statement depending on the given condition.\nWhen applied to switch statements: Compile the statement as a series of if statements each with the branch attribute."));
            allAttributes.Add(Create("call", "The bodies of the individual cases in the switch will be moved into hardware subroutines and the switch will be a series of subroutine calls."));
            allAttributes.Add(Create("fastopt", "Reduces the compile time but produces less aggressive optimizations. If you use this attribute, the compiler will not unroll loops."));
            allAttributes.Add(Create("flatten", "When applied to if statements: Evaluate both sides of the if statement and choose between the two resulting values.\nWhen applied to switch statements: Compile the statement as a series of if statements, each with the flatten attribute."));
            allAttributes.Add(Create("forcecase", "Force a switch statement in the hardware."));
            allAttributes.Add(Create("loop", "Generate code that uses flow control to execute each iteration of the loop. Not compatible with the [unroll] attribute."));

            allAttributes.Add(Create("unroll", "Unroll the loop until it stops executing. Can optionally specify the maximum number of times the loop is to execute. Not compatible with the [loop] attribute.", a => new[]
            {
                new ParameterSymbol("count", "Maximum number of times the loop is to execute.", a, IntrinsicTypes.Int)
            }));

            allAttributes.Add(Create("clipplanes", "", a => new[]
            {
                new ParameterSymbol("clipPlane1", "", a, IntrinsicTypes.Float4),
                new ParameterSymbol("clipPlane2", "", a, IntrinsicTypes.Float4),
                new ParameterSymbol("clipPlane3", "", a, IntrinsicTypes.Float4),
                new ParameterSymbol("clipPlane4", "", a, IntrinsicTypes.Float4),
                new ParameterSymbol("clipPlane5", "", a, IntrinsicTypes.Float4),
                new ParameterSymbol("clipPlane6", "", a, IntrinsicTypes.Float4)
            }));

            allAttributes.Add(Create("domain", "Defines the patch type used in the HS.", a => new[]
            {
                new ParameterSymbol("domainType", "", a, IntrinsicTypes.String)
            }));

            allAttributes.Add(Create("earlydepthstencil", "Forces depth-stencil testing before a shader executes."));

            allAttributes.Add(Create("instance", "Use this attribute to instance a geometry shader.", a => new[]
            {
                new ParameterSymbol("count", "An integer index that indicates the number of instances to be executed for each drawn item (for example, for each triangle).", a, IntrinsicTypes.Int)
            }));

            allAttributes.Add(Create("maxtessfactor", "Indicates the maximum value that the hull shader would return for any tessellation factor.", a => new[]
            {
                new ParameterSymbol("factor", "Upper bound on the amount of tessellation requested to help a driver determine the maximum amount of resources required for tessellation.", a, IntrinsicTypes.Int)
            }));

            allAttributes.Add(Create("numthreads", "Defines the number of threads to be executed in a single thread group when a compute shader is dispatched.", a => new[]
            {
                new ParameterSymbol("x", "Size of the thread group in the X direction.", a, IntrinsicTypes.Int),
                new ParameterSymbol("y", "Size of the thread group in the Y direction.", a, IntrinsicTypes.Int),
                new ParameterSymbol("z", "Size of the thread group in the Z direction.", a, IntrinsicTypes.Int)
            }));

            allAttributes.Add(Create("outputcontrolpoints", "Defines the number of output control points (per thread) that will be created in the hull shader.", a => new[]
            {
                new ParameterSymbol("count", "", a, IntrinsicTypes.Int)
            }));

            allAttributes.Add(Create("outputtopology", "Defines the output primitive type for the tessellator.", a => new[]
            {
                new ParameterSymbol("topology", "Must be one of \"point\", \"line\", \"triangle_cw\", or \"triangle_ccw\".", a, IntrinsicTypes.String)
            }));

            allAttributes.Add(Create("partioning", "Defines the tesselation scheme to be used in the hull shader.", a => new[]
            {
                new ParameterSymbol("scheme", "Must be one of \"integer\", \"fractional_even\", \"fractional_odd\", or \"pow2\".", a, IntrinsicTypes.String)
            }));

            allAttributes.Add(Create("patchconstantfunc", "Defines the function for computing patch constant data.", a => new[]
            {
                new ParameterSymbol("functionName", "Name of a separate function that outputs the patch-constant data.", a, IntrinsicTypes.String)
            }));

            allAttributes.Add(Create("maxvertexcount", "Declares the maximum number of vertices to create.", a => new[]
            {
                new ParameterSymbol("count", "Maximum number of vertices.", a, IntrinsicTypes.Int)
            }));

            allAttributes.Add(Create("intrinsic", "", a => new[]
            {
                new ParameterSymbol("group", "", a, IntrinsicTypes.String),
                new ParameterSymbol("opcode", "", a, IntrinsicTypes.Int)
            }));

            allAttributes.Add(Create("RootSignature", "Defines the root signature.", a => new[]
            {
                new ParameterSymbol("signature", "", a, IntrinsicTypes.String)
            }));

            AllAttributes = allAttributes.ToImmutableArray();
        }
Example #38
0
        /// <summary>
        /// Build up the <see cref="ZipData"/> instance for a given zip container. This will also report any consistency
        /// errors found when examining the zip archive.
        /// </summary>
        private bool TryBuildZipData(FileSignInfo zipFileSignInfo, out ZipData zipData)
        {
            Debug.Assert(zipFileSignInfo.IsZipContainer());

            try
            {
                using (var archive = new ZipArchive(File.OpenRead(zipFileSignInfo.FullPath), ZipArchiveMode.Read))
                {
                    var nestedParts = new List <ZipPart>();

                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        string relativePath = entry.FullName;

                        // `entry` might be just a pointer to a folder. We skip those.
                        if (relativePath.EndsWith("/") && entry.Name == "")
                        {
                            continue;
                        }

                        ImmutableArray <byte> contentHash;
                        using (var stream = entry.Open())
                        {
                            contentHash = ContentUtil.GetContentHash(stream);
                        }

                        var fileUniqueKey = new SignedFileContentKey(contentHash, relativePath);

                        if (!_whichPackagesTheFileIsIn.TryGetValue(fileUniqueKey, out var packages))
                        {
                            packages = new HashSet <string>();
                        }

                        packages.Add(zipFileSignInfo.FileName);
                        _whichPackagesTheFileIsIn[fileUniqueKey] = packages;

                        // if we already encountered file that hash the same content we can reuse its signed version when repackaging the container.
                        var fileName = Path.GetFileName(relativePath);
                        if (!_filesByContentKey.TryGetValue(new SignedFileContentKey(contentHash, fileName), out var fileSignInfo))
                        {
                            string extractPathRoot = _useHashInExtractionPath ? ContentUtil.HashToString(contentHash) : _filesByContentKey.Count().ToString();
                            string tempPath        = Path.Combine(_pathToContainerUnpackingDirectory, extractPathRoot, relativePath);
                            _log.LogMessage($"Extracting file '{fileName}' from '{zipFileSignInfo.FullPath}' to '{tempPath}'.");

                            Directory.CreateDirectory(Path.GetDirectoryName(tempPath));

                            using (var stream = entry.Open())
                                using (var tempFileStream = File.OpenWrite(tempPath))
                                {
                                    stream.CopyTo(tempFileStream);
                                }

                            fileSignInfo = TrackFile(tempPath, contentHash, isNested: true);
                        }

                        if (fileSignInfo.SignInfo.ShouldSign || fileSignInfo.ForceRepack)
                        {
                            nestedParts.Add(new ZipPart(relativePath, fileSignInfo));
                        }
                    }

                    zipData = new ZipData(zipFileSignInfo, nestedParts.ToImmutableArray());

                    return(true);
                }
            }
            catch (Exception e)
            {
                _log.LogErrorFromException(e);
                zipData = null;
                return(false);
            }
        }
Example #39
0
        /*
         * Sky
         * http://vterrain.org/Atmosphere/index.html
         *
         * https://gamedev.ru/code/forum/?id=19689
         * http://www.philohome.com/skycollec/skycollec.htm
         * http://steps3d.narod.ru/tutorials/sky-tutorial.html
         */

        public static SkyGameObject Create(IEntityManager manager)
        {
            var resources = Path.Combine(@"Resources\sky\");
            //../../../../D3DLab.Wpf.Engine.App/Resources/sky/
            var tag   = new ElementTag("SkyDome");
            var plane = new ElementTag("SkyPlane");

            //var geo = GenerateSphere(2, Vector3.Zero, 15f.ToRad());
            var file   = new FileInfo(Path.Combine(resources, "sky.obj"));
            var points = new List <Vector3>();
            var indx   = new List <int>();

            var index = 0;

            foreach (var line in File.ReadAllLines(file.FullName))
            {
                var parts = line.Split(' ');
                points.Add(new Vector3(float.Parse(parts[1]), float.Parse(parts[2]), float.Parse(parts[3])));

                indx.Add(index);
                index++;
            }


            var geo = new HittableGeometryComponent();

            geo.Positions = points.ToImmutableArray();
            geo.Indices   = indx.ToImmutableArray();
            geo.Color     = V4Colors.White;
            //geo.TextureCoordinates = geo.Positions.Select(x => new Vector2()).ToImmutableArray();

            manager.CreateEntity(tag)
            .AddComponents(new IGraphicComponent[] {
                new D3DSkyRenderComponent()
                {
                },
                geo,
                TransformComponent.Identity(),
                new GradientMaterialComponent(),
                new FollowUpCameraPositionComponent()
            });

            var planemesh = GeometryBuilder.BuildSkyPlane(SkyPlaneData.Default);

            manager.CreateEntity(plane)
            .AddComponents(new IGraphicComponent[] {
                new D3DSkyPlaneRenderComponent(),
                new D3DTexturedMaterialSamplerComponent(
                    new System.IO.FileInfo(Path.Combine(resources, "cloud001.bmp")),
                    new System.IO.FileInfo(Path.Combine(resources, "cloud002.bmp"))
                    ),
                new SimpleGeometryComponent {
                    Positions          = planemesh.Positions.ToImmutableArray(),
                    Indices            = planemesh.Indices.ToImmutableArray(),
                    TextureCoordinates = planemesh.TextureCoordinates.ToImmutableArray(),
                },
                TransformComponent.Identity(),
                new FollowUpCameraPositionComponent(),
                new SkyPlaneParallaxAnimationComponent()
            });

            return(new SkyGameObject(tag));
        }
Example #40
0
        internal BatchSignInput GenerateListOfFiles()
        {
            foreach (var fullPath in _itemsToSign)
            {
                var fileUniqueKey = new SignedFileContentKey(ContentUtil.GetContentHash(fullPath), fullPath);

                if (!_whichPackagesTheFileIsIn.TryGetValue(fileUniqueKey, out var packages))
                {
                    packages = new HashSet <string>();
                }

                packages.Add(fullPath);
                _whichPackagesTheFileIsIn[fileUniqueKey] = packages;

                TrackFile(fullPath, ContentUtil.GetContentHash(fullPath), isNested: false);
            }

            if (_errors.Any())
            {
                // Iterate over each pair of <error code, unique file identity>.
                // We can be sure here that the same file won't have the same error code twice.
                foreach (var errorGroup in _errors)
                {
                    switch (errorGroup.Key)
                    {
                    case SigningToolErrorCode.SIGN002:
                        _log.LogError("Could not determine certificate name for signable file(s):");
                        break;
                    }

                    // For each file that had that error
                    foreach (var erroedFile in errorGroup.Value)
                    {
                        _log.LogError($"\tFile: {erroedFile.FileName}");

                        // Get a list of all containers where the file showed up
                        foreach (var containerName in _whichPackagesTheFileIsIn[erroedFile])
                        {
                            _log.LogError($"\t\t{containerName}");
                        }
                    }
                }
            }

            return(new BatchSignInput(_filesToSign.ToImmutableArray(), _zipDataMap.ToImmutableDictionary(ByteSequenceComparer.Instance), _filesToCopy.ToImmutableArray()));
        }
Example #41
0
        static async Task <ImmutableArray <ICodeFix> > GetCodeFixes(
            Document document, string codeFixName,
            ImmutableArray <DiagnosticAnalyzer> analyzers = default, CancellationToken cancellationToken = default)
        {
            var provider = GetCodeFixProvider(document, codeFixName);

            if (provider == null)
            {
                return(ImmutableArray <ICodeFix> .Empty);
            }

            var compilation = await document.Project.GetCompilationAsync(cancellationToken);

            // TODO: should we allow extending the set of built-in analyzers being added?
            if (analyzers.IsDefaultOrEmpty)
            {
                analyzers = builtInAnalyzers.Value;
            }

            var supportedAnalyers = analyzers
                                    .Where(a => a.SupportedDiagnostics.Any(d => provider.FixableDiagnosticIds.Contains(d.Id)))
                                    .ToImmutableArray();

            var allDiagnostics = default(ImmutableArray <Diagnostic>);

            // This may be a compiler warning/error, not an analyzer-produced one, such as
            // the missing abstract method implementations.
            if (supportedAnalyers.IsEmpty)
            {
                allDiagnostics = compilation.GetDiagnostics(cancellationToken);
            }
            else
            {
                var analyerCompilation = compilation.WithAnalyzers(supportedAnalyers, cancellationToken: cancellationToken);
                allDiagnostics = await analyerCompilation.GetAllDiagnosticsAsync(cancellationToken);
            }

            var diagnostics = allDiagnostics
                              .Where(x => provider.FixableDiagnosticIds.Contains(x.Id))
                              // Only consider the diagnostics raised by the target document.
                              .Where(d =>
                                     d.Location.Kind == LocationKind.SourceFile &&
                                     d.Location.GetLineSpan().Path == document.FilePath);

            var codeFixes = new List <ICodeFix>();

            foreach (var diagnostic in diagnostics)
            {
                await provider.RegisterCodeFixesAsync(
                    new CodeFixContext(document, diagnostic,
                                       (action, diag) => codeFixes.Add(new CodeFixAdapter(action, diag, codeFixName)),
                                       cancellationToken));
            }

            var finalFixes = new List <ICodeFix>();

            // All code actions without equivalence keys must be applied individually.
            finalFixes.AddRange(codeFixes.Where(x => x.Action.EquivalenceKey == null));
            // All code actions with the same equivalence key should be applied only once.
            finalFixes.AddRange(codeFixes
                                .Where(x => x.Action.EquivalenceKey != null)
                                .GroupBy(x => x.Action.EquivalenceKey)
                                .Select(x => x.First()));

            return(finalFixes.ToImmutableArray());
        }
Example #42
0
        private async Task TestFixAllWithMultipleEncodingsAsync(FixAllScope scope)
        {
            string[] testCode = new[] { "class Foo { }", "class Bar { }" };

            this.fileEncoding = Encoding.Unicode;

            // Create a project using the specified encoding
            Project project = this.CreateProject(testCode);

            project = project.AddDocument("Test2.cs", SourceText.From("class FooBar { }", Encoding.UTF7)).Project;

            Project oldProject = project;

            Workspace workspace = project.Solution.Workspace;

            var codeFixer      = this.GetCSharpCodeFixProvider();
            var fixAllProvider = codeFixer.GetFixAllProvider();
            var diagnostics    = new List <Diagnostic>();
            var descriptor     = this.GetCSharpDiagnosticAnalyzers().First().SupportedDiagnostics.First();

            foreach (var document in project.Documents)
            {
                // Create a diagnostic for the document to fix
                var properties = ImmutableDictionary <string, string> .Empty.SetItem(SA1412StoreFilesAsUtf8.EncodingProperty, (await document.GetTextAsync().ConfigureAwait(false)).Encoding.WebName);

                var diagnostic = Diagnostic.Create(
                    descriptor,
                    Location.Create(await document.GetSyntaxTreeAsync().ConfigureAwait(false), TextSpan.FromBounds(0, 0)),
                    properties);
                diagnostics.Add(diagnostic);
            }

            FixAllContext fixAllContext = new FixAllContext(
                project.Documents.First(),
                codeFixer,
                scope,
                nameof(SA1412CodeFixProvider) + "." + this.fileEncoding.WebName,
                new[] { SA1412StoreFilesAsUtf8.DiagnosticId },
                TestDiagnosticProvider.Create(diagnostics.ToImmutableArray()),
                CancellationToken.None);

            CodeAction codeAction = await fixAllProvider.GetFixAsync(fixAllContext).ConfigureAwait(false);

            var operation = codeAction.GetOperationsAsync(CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult()[0];

            operation.Apply(workspace, CancellationToken.None);

            // project should now have the "fixed document" in it.
            // Because of limitations in Roslyn the fixed document should
            // have a different DocumentId then the broken document
            project = workspace.CurrentSolution.Projects.First();

            Assert.Equal(3, project.DocumentIds.Count);

            // The order of the document ids is now different from the order before.
            for (int i = 1; i < 3; i++)
            {
                DocumentId documentId = project.DocumentIds[i];
                SourceText sourceText = await project.GetDocument(documentId).GetTextAsync().ConfigureAwait(false);

                Assert.Equal(testCode[i - 1], sourceText.ToString());

                Assert.Equal(Encoding.UTF8, sourceText.Encoding);
                Assert.NotEqual(oldProject.DocumentIds[i - 1], project.DocumentIds[i]);
            }

            // Check that Test2.cs was not changed
            DocumentId otherDocumentId = project.DocumentIds[0];
            var        otherDocument   = project.GetDocument(otherDocumentId);

            Assert.Equal("Test2.cs", otherDocument.Name);

            SourceText otherDocumentText = await otherDocument.GetTextAsync().ConfigureAwait(false);

            Assert.Equal("class FooBar { }", otherDocumentText.ToString());

            Assert.Equal(Encoding.UTF7, otherDocumentText.Encoding);
            Assert.Equal(oldProject.DocumentIds[2], project.DocumentIds[0]);
        }
Example #43
0
        public override void VisitSwitchStmt(SwitchStmt x)
        {
            var items = x.SwitchItems;

            if (items == null || items.Length == 0)
            {
                return;
            }

            // get bound item for switch value & connect potential pre-switch-value blocks
            var boundBagForSwitchValue = _binder.BindWholeExpression(x.SwitchValue, BoundAccess.Read);

            ConnectBoundItemsBagBlocksToCurrentBlock(boundBagForSwitchValue);
            var switchValue = boundBagForSwitchValue.BoundElement;

            var end = NewBlock();

            bool hasDefault = false;
            var  cases      = new List <CaseBlock>(items.Length);

            for (int i = 0; i < items.Length; i++)
            {
                cases.Add(NewBlock(items[i]));
                hasDefault |= (items[i] is DefaultItem);
            }
            if (!hasDefault)
            {
                // create implicit default:
                cases.Add(NewBlock(new DefaultItem(x.Span, EmptyArray <Statement> .Instance)));
            }

            // if switch value isn't a constant & there're case values with preBoundStatements
            // -> the switch value might get evaluated multiple times (see SwitchEdge.Generate) -> preemptively evaluate and cache it
            if (!switchValue.IsConstant() && !cases.All(c => c.CaseValue.IsOnlyBoundElement))
            {
                var result = GeneratorSemanticsBinder.CreateAndAssignSynthesizedVariable(switchValue, BoundAccess.Read, $"<switchValueCacher>{x.Span}");
                switchValue = result.BoundExpr;
                _current.Add(new BoundExpressionStatement(result.Assignment));
            }

            // SwitchEdge // Connects _current to cases
            var edge = new SwitchEdge(_current, switchValue, cases.ToImmutableArray(), end);

            _current = WithNewOrdinal(cases[0]);

            OpenBreakScope(end, end); // NOTE: inside switch, Continue ~ Break

            for (int i = 0; i < cases.Count; i++)
            {
                OpenScope(_current);

                if (i < items.Length)
                {
                    items[i].Statements.ForEach(VisitElement);  // any break will connect block to end
                }
                CloseScope();

                _current = WithNewOrdinal(Connect(_current, (i == cases.Count - 1) ? end : cases[i + 1]));
            }

            CloseBreakScope();

            Debug.Assert(_current == end);
        }
        GetImportanceMetricsMatrix(
            IHostEnvironment env,
            IPredictionTransformer <TModel> model,
            IDataView data,
            Func <TResult> resultInitializer,
            Func <IDataView, TMetric> evaluationFunc,
            Func <TMetric, TMetric, TMetric> deltaFunc,
            string features,
            int permutationCount,
            bool useFeatureWeightFilter = false,
            int?topExamples             = null)
        {
            Contracts.CheckValue(env, nameof(env));
            var host = env.Register(nameof(PermutationFeatureImportance <TModel, TMetric, TResult>));

            host.CheckValue(model, nameof(model));
            host.CheckValue(data, nameof(data));
            host.CheckNonEmpty(features, nameof(features));

            topExamples = topExamples ?? Utils.ArrayMaxSize;
            host.Check(topExamples > 0, "Provide how many examples to use (positive number) or set to null to use whole dataset.");

            VBuffer <ReadOnlyMemory <char> > slotNames = default;
            var metricsDelta = new List <TResult>();

            using (var ch = host.Start("GetImportanceMetrics"))
            {
                ch.Trace("Scoring and evaluating baseline.");
                var baselineMetrics = evaluationFunc(model.Transform(data));

                // Get slot names.
                var featuresColumn = data.Schema[features];
                int numSlots       = featuresColumn.Type.GetVectorSize();
                data.Schema.TryGetColumnIndex(features, out int featuresColumnIndex);

                ch.Info("Number of slots: " + numSlots);
                if (data.Schema[featuresColumnIndex].HasSlotNames(numSlots))
                {
                    data.Schema[featuresColumnIndex].Annotations.GetValue(AnnotationUtils.Kinds.SlotNames, ref slotNames);
                }

                if (slotNames.Length != numSlots)
                {
                    slotNames = VBufferUtils.CreateEmpty <ReadOnlyMemory <char> >(numSlots);
                }

                VBuffer <float> weights = default;
                var             workingFeatureIndices = Enumerable.Range(0, numSlots).ToList();
                int             zeroWeightsCount      = 0;

                // By default set to the number of all features available.
                var evaluatedFeaturesCount = numSlots;
                if (useFeatureWeightFilter)
                {
                    var predictorWithWeights = model.Model as IPredictorWithFeatureWeights <Single>;
                    if (predictorWithWeights != null)
                    {
                        predictorWithWeights.GetFeatureWeights(ref weights);

                        const int     maxReportedZeroFeatures = 10;
                        StringBuilder msgFilteredOutFeatures  = new StringBuilder("The following features have zero weight and will not be evaluated: \n \t");
                        var           prefix = "";
                        foreach (var k in weights.Items(all: true))
                        {
                            if (k.Value == 0)
                            {
                                zeroWeightsCount++;

                                // Print info about first few features we're not going to evaluate.
                                if (zeroWeightsCount <= maxReportedZeroFeatures)
                                {
                                    msgFilteredOutFeatures.Append(prefix);
                                    msgFilteredOutFeatures.Append(GetSlotName(slotNames, k.Key));
                                    prefix = ", ";
                                }
                            }
                            else
                            {
                                workingFeatureIndices.Add(k.Key);
                            }
                        }

                        // Old FastTree models has less weights than slots.
                        if (weights.Length < numSlots)
                        {
                            ch.Warning(
                                "Predictor had fewer features than slots. All unknown features will get default 0 weight.");
                            zeroWeightsCount += numSlots - weights.Length;
                            var indexes = weights.GetIndices().ToArray();
                            var values  = weights.GetValues().ToArray();
                            var count   = values.Length;
                            weights = new VBuffer <float>(numSlots, count, values, indexes);
                        }

                        evaluatedFeaturesCount = workingFeatureIndices.Count;
                        ch.Info("Number of zero weights: {0} out of {1}.", zeroWeightsCount, weights.Length);

                        // Print what features have 0 weight
                        if (zeroWeightsCount > 0)
                        {
                            if (zeroWeightsCount > maxReportedZeroFeatures)
                            {
                                msgFilteredOutFeatures.Append(string.Format("... (printing out  {0} features here).\n Use 'Index' column in the report for info on what features are not evaluated.", maxReportedZeroFeatures));
                            }
                            ch.Info(msgFilteredOutFeatures.ToString());
                        }
                    }
                }

                if (workingFeatureIndices.Count == 0 && zeroWeightsCount == 0)
                {
                    // Use all features otherwise.
                    workingFeatureIndices.AddRange(Enumerable.Range(0, numSlots));
                }

                if (zeroWeightsCount == numSlots)
                {
                    ch.Warning("All features have 0 weight thus can not do thorough evaluation");
                    return(metricsDelta.ToImmutableArray());
                }

                // Note: this will not work on the huge dataset.
                var          maxSize = topExamples;
                List <float> initialfeatureValuesList = new List <float>();

                // Cursor through the data to cache slot 0 values for the upcoming permutation.
                var valuesRowCount = 0;
                // REVIEW: Seems like if the labels are NaN, so that all metrics are NaN, this command will be useless.
                // In which case probably erroring out is probably the most useful thing.
                using (var cursor = data.GetRowCursor(featuresColumn))
                {
                    var featuresGetter = cursor.GetGetter <VBuffer <float> >(featuresColumn);
                    var featuresBuffer = default(VBuffer <float>);

                    while (initialfeatureValuesList.Count < maxSize && cursor.MoveNext())
                    {
                        featuresGetter(ref featuresBuffer);
                        initialfeatureValuesList.Add(featuresBuffer.GetItemOrDefault(workingFeatureIndices[0]));
                    }

                    valuesRowCount = initialfeatureValuesList.Count;
                }

                if (valuesRowCount > 0)
                {
                    ch.Info("Detected {0} examples for evaluation.", valuesRowCount);
                }
                else
                {
                    ch.Warning("Detected no examples for evaluation.");
                    return(metricsDelta.ToImmutableArray());
                }

                float[] featureValuesBuffer = initialfeatureValuesList.ToArray();
                float[] nextValues          = new float[valuesRowCount];

                // Now iterate through all the working slots, do permutation and calc the delta of metrics.
                int processedCnt     = 0;
                int nextFeatureIndex = 0;
                var shuffleRand      = RandomUtils.Create(host.Rand.Next());
                using (var pch = host.StartProgressChannel("SDCA preprocessing with lookup"))
                {
                    pch.SetHeader(new ProgressHeader("processed slots"), e => e.SetProgress(0, processedCnt));
                    foreach (var workingIndx in workingFeatureIndices)
                    {
                        // Index for the feature we will permute next.  Needed to build in advance a buffer for the permutation.
                        if (processedCnt < workingFeatureIndices.Count - 1)
                        {
                            nextFeatureIndex = workingFeatureIndices[processedCnt + 1];
                        }

                        // Used for pre-caching the next feature
                        int nextValuesIndex = 0;

                        SchemaDefinition input = SchemaDefinition.Create(typeof(FeaturesBuffer));
                        Contracts.Assert(input.Count == 1);
                        input[0].ColumnName = features;

                        SchemaDefinition output = SchemaDefinition.Create(typeof(FeaturesBuffer));
                        Contracts.Assert(output.Count == 1);
                        output[0].ColumnName = features;
                        output[0].ColumnType = featuresColumn.Type;

                        // Perform multiple permutations for one feature to build a confidence interval
                        var metricsDeltaForFeature = resultInitializer();
                        for (int permutationIteration = 0; permutationIteration < permutationCount; permutationIteration++)
                        {
                            Utils.Shuffle <float>(shuffleRand, featureValuesBuffer);

                            Action <FeaturesBuffer, FeaturesBuffer, PermuterState> permuter =
                                (src, dst, state) =>
                            {
                                src.Features.CopyTo(ref dst.Features);
                                VBufferUtils.ApplyAt(ref dst.Features, workingIndx,
                                                     (int ii, ref float d) =>
                                                     d = featureValuesBuffer[state.SampleIndex++]);

                                // Is it time to pre-cache the next feature?
                                if (permutationIteration == permutationCount - 1 &&
                                    processedCnt < workingFeatureIndices.Count - 1)
                                {
                                    // Fill out the featureValueBuffer for the next feature while updating the current feature
                                    // This is the reason I need PermuterState in LambdaTransform.CreateMap.
                                    nextValues[nextValuesIndex] = src.Features.GetItemOrDefault(nextFeatureIndex);
                                    if (nextValuesIndex < valuesRowCount - 1)
                                    {
                                        nextValuesIndex++;
                                    }
                                }
                            };

                            IDataView viewPermuted = LambdaTransform.CreateMap(
                                host, data, permuter, null, input, output);
                            if (valuesRowCount == topExamples)
                            {
                                viewPermuted = SkipTakeFilter.Create(host, new SkipTakeFilter.TakeOptions()
                                {
                                    Count = valuesRowCount
                                }, viewPermuted);
                            }

                            var metrics = evaluationFunc(model.Transform(viewPermuted));

                            var delta = deltaFunc(metrics, baselineMetrics);
                            metricsDeltaForFeature.Add(delta);
                        }

                        // Add the metrics delta to the list
                        metricsDelta.Add(metricsDeltaForFeature);

                        // Swap values for next iteration of permutation.
                        if (processedCnt < workingFeatureIndices.Count - 1)
                        {
                            Array.Clear(featureValuesBuffer, 0, featureValuesBuffer.Length);
                            nextValues.CopyTo(featureValuesBuffer, 0);
                            Array.Clear(nextValues, 0, nextValues.Length);
                        }
                        processedCnt++;
                    }
                    pch.Checkpoint(processedCnt, processedCnt);
                }
            }

            return(metricsDelta.ToImmutableArray());
        }
Example #45
0
        private BoundInterfaceType BindInterfaceDeclaration(InterfaceTypeSyntax declaration, Symbol parent)
        {
            var interfaceSymbol = new InterfaceSymbol(declaration, parent);
            AddSymbol(interfaceSymbol, declaration.Name.Span);

            var methods = new List<BoundFunction>();
            var interfaceBinder = new Binder(_sharedBinderState, this);
            foreach (var memberSyntax in declaration.Methods)
                methods.Add(interfaceBinder.Bind(memberSyntax, x => interfaceBinder.BindFunctionDeclaration(x, interfaceSymbol)));

            foreach (var member in interfaceBinder.LocalSymbols.Values.SelectMany(x => x))
                interfaceSymbol.AddMember(member);

            return new BoundInterfaceType(interfaceSymbol, methods.ToImmutableArray());
        }
 /// <summary>
 /// Gets the files which should be removed.
 /// </summary>
 /// <returns></returns>
 public ImmutableArray <string> GetFilesToRemove()
 {
     return(FilesToRemove.ToImmutableArray());
 }
Example #47
0
        private BoundTypeAlias BindTypeAlias(TypeAliasSyntax syntax, TypeSymbol variableType)
        {
            variableType = BindArrayRankSpecifiers(syntax.ArrayRankSpecifiers, variableType);

            var symbol = new TypeAliasSymbol(syntax, variableType);
            AddSymbol(symbol, syntax.Identifier.Span);

            var boundQualifiers = new List<BoundVariableQualifier>();
            foreach (var qualifier in syntax.Qualifiers)
                boundQualifiers.Add(Bind(qualifier, BindVariableQualifier));

            return new BoundTypeAlias(symbol, variableType, boundQualifiers.ToImmutableArray());
        }
 /// <summary>
 /// Gets the files which should be ignored when removing files.
 /// </summary>
 /// <returns></returns>
 public ImmutableArray <string> GetFilesToIgnore()
 {
     return(FilesToIgnore.ToImmutableArray());
 }
        private async Task TestFixAllWithMultipleEncodingsAsync(FixAllScope scope)
        {
            string[] testCode = new[] { "class Foo { }", "class Bar { }" };

            this.fileEncoding = Encoding.Unicode;

            // Create a project using the specified encoding
            Project project = this.CreateProject(testCode);

            project = project.AddDocument("Test2.cs", SourceText.From("class FooBar { }", Encoding.UTF7)).Project;

            Project oldProject = project;

            Workspace workspace = project.Solution.Workspace;

            var codeFixer = this.GetCSharpCodeFixProvider();
            var fixAllProvider = codeFixer.GetFixAllProvider();
            var diagnostics = new List<Diagnostic>();
            var descriptor = this.GetCSharpDiagnosticAnalyzers().First().SupportedDiagnostics.First();
            foreach (var document in project.Documents)
            {
                // Create a diagnostic for the document to fix
                var properties = ImmutableDictionary<string, string>.Empty.SetItem(SA1412StoreFilesAsUtf8.EncodingProperty, (await document.GetTextAsync().ConfigureAwait(false)).Encoding.WebName);
                var diagnostic = Diagnostic.Create(
                    descriptor,
                    Location.Create(await document.GetSyntaxTreeAsync().ConfigureAwait(false), TextSpan.FromBounds(0, 0)),
                    properties);
                diagnostics.Add(diagnostic);
            }

            FixAllContext fixAllContext = new FixAllContext(
                project.Documents.First(),
                codeFixer,
                scope,
                nameof(SA1412CodeFixProvider) + "." + this.fileEncoding.WebName,
                new[] { SA1412StoreFilesAsUtf8.DiagnosticId },
                TestDiagnosticProvider.Create(diagnostics.ToImmutableArray()),
                CancellationToken.None);

            CodeAction codeAction = await fixAllProvider.GetFixAsync(fixAllContext).ConfigureAwait(false);
            var operation = codeAction.GetOperationsAsync(CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult()[0];

            operation.Apply(workspace, CancellationToken.None);

            // project should now have the "fixed document" in it.
            // Because of limitations in Roslyn the fixed document should
            // have a different DocumentId then the broken document
            project = workspace.CurrentSolution.Projects.First();

            Assert.Equal(3, project.DocumentIds.Count);

            // The order of the document ids is now different from the order before.
            for (int i = 1; i < 3; i++)
            {
                DocumentId documentId = project.DocumentIds[i];
                SourceText sourceText = await project.GetDocument(documentId).GetTextAsync().ConfigureAwait(false);

                Assert.Equal(testCode[i - 1], sourceText.ToString());

                Assert.Equal(Encoding.UTF8, sourceText.Encoding);
                Assert.NotEqual(oldProject.DocumentIds[i - 1], project.DocumentIds[i]);
            }

            // Check that Test2.cs was not changed
            DocumentId otherDocumentId = project.DocumentIds[0];
            var otherDocument = project.GetDocument(otherDocumentId);

            Assert.Equal("Test2.cs", otherDocument.Name);

            SourceText otherDocumentText = await otherDocument.GetTextAsync().ConfigureAwait(false);

            Assert.Equal("class FooBar { }", otherDocumentText.ToString());

            Assert.Equal(Encoding.UTF7, otherDocumentText.Encoding);
            Assert.Equal(oldProject.DocumentIds[2], project.DocumentIds[0]);
        }
 /// <summary>
 /// Gets the files which were copied from en-us repository.
 /// </summary>
 /// <returns></returns>
 public ImmutableArray <string> GetCopiedFiles()
 {
     return(CopiedFiles.ToImmutableArray());
 }
Example #51
0
            public override TextParserResult GetSpans(string s, IComparer <LinePositionSpanInfo> comparer = null)
            {
                StringBuilder sb = StringBuilderCache.GetInstance(s.Length - TokensLength);

                var startPending = false;
                LinePositionInfo            start = default;
                Stack <LinePositionInfo>    stack = null;
                List <LinePositionSpanInfo> spans = null;

                int lastPos = 0;

                int line   = 0;
                int column = 0;

                int length = s.Length;

                int i = 0;

                while (i < length)
                {
                    switch (s[i])
                    {
                    case '\r':
                    {
                        if (PeekNextChar() == '\n')
                        {
                            i++;
                        }

                        line++;
                        column = 0;
                        i++;
                        continue;
                    }

                    case '\n':
                    {
                        line++;
                        column = 0;
                        i++;
                        continue;
                    }

                    case '[':
                    {
                        char nextChar = PeekNextChar();
                        if (nextChar == '|')
                        {
                            sb.Append(s, lastPos, i - lastPos);

                            var start2 = new LinePositionInfo(sb.Length, line, column);

                            if (stack != null)
                            {
                                stack.Push(start2);
                            }
                            else if (!startPending)
                            {
                                start        = start2;
                                startPending = true;
                            }
                            else
                            {
                                stack = new Stack <LinePositionInfo>();
                                stack.Push(start);
                                stack.Push(start2);
                                startPending = false;
                            }

                            i      += 2;
                            lastPos = i;
                            continue;
                        }
                        else if (nextChar == '[' &&
                                 PeekChar(2) == '|' &&
                                 PeekChar(3) == ']')
                        {
                            i++;
                            column++;
                            CloseSpan();
                            i      += 3;
                            lastPos = i;
                            continue;
                        }

                        break;
                    }

                    case '|':
                    {
                        if (PeekNextChar() == ']')
                        {
                            CloseSpan();
                            i      += 2;
                            lastPos = i;
                            continue;
                        }

                        break;
                    }
                    }

                    column++;
                    i++;
                }

                if (startPending ||
                    stack?.Count > 0)
                {
                    throw new InvalidOperationException();
                }

                sb.Append(s, lastPos, s.Length - lastPos);

                spans?.Sort(comparer ?? LinePositionSpanInfoComparer.Index);

                return(new TextParserResult(
                           StringBuilderCache.GetStringAndFree(sb),
                           spans?.ToImmutableArray() ?? ImmutableArray <LinePositionSpanInfo> .Empty));

                char PeekNextChar()
                {
                    return(PeekChar(1));
                }

                char PeekChar(int offset)
                {
                    return((i + offset >= s.Length) ? '\0' : s[i + offset]);
                }

                void CloseSpan()
                {
                    if (stack != null)
                    {
                        start = stack.Pop();
                    }
                    else if (startPending)
                    {
                        startPending = false;
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }

                    var end = new LinePositionInfo(sb.Length + i - lastPos, line, column);

                    var span = new LinePositionSpanInfo(start, end);

                    (spans ??= new List <LinePositionSpanInfo>()).Add(span);

                    sb.Append(s, lastPos, i - lastPos);
                }
            }
Example #52
0
        public SyntaxToken Lex(LexerMode mode)
        {
            // First check if we're in the middle of expanding a macro reference token.
            if (_expandedMacroTokens != null)
            {
                var result = _expandedMacroTokens[_expandedMacroIndex++];
                if (_expandedMacroIndex == _expandedMacroTokens.Count)
                {
                    _expandedMacroTokens = null;
                }
                return(result);
            }

            _mode = mode;

            SyntaxToken token;

            switch (_mode)
            {
            case LexerMode.Syntax:
                token = LexSyntaxToken();
                break;

            case LexerMode.Directive:
                token = LexDirectiveToken();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            // Swallow end-of-file tokens from include files.
            if (token.Kind == SyntaxKind.EndOfFileToken && _includeStack.Count > 1)
            {
                var originalToken = token;

                PopIncludeContext();
                token = Lex(mode);
                token = token.WithLeadingTrivia(originalToken.LeadingTrivia.AddRange(token.LeadingTrivia));

                // this is a bit weird, but we need to also update the leading trivia on the macro reference,
                // because that's what we use when outputting code.
                if (token.MacroReference != null)
                {
                    token = token.WithOriginalMacroReference(token.MacroReference.WithLeadingTrivia(token.LeadingTrivia), token.IsFirstTokenInMacroExpansion);
                }
            }

            // Expand macros and attach as a special kind of trivia.
            if (token.Kind == SyntaxKind.IdentifierToken && ExpandMacros)
            {
                List <SyntaxToken> expandedTokens;
                if (TryExpandMacro(token, new BaseMacroExpansionLexer(this), out expandedTokens))
                {
                    if (expandedTokens.Count == 0) // Can happen for macros with empty body.
                    {
                        // Attach macro call as leading trivia on next token.
                        var originalToken = token;
                        token = Lex(mode);

                        var leadingTrivia = new List <SyntaxNode>();
                        leadingTrivia.AddRange(originalToken.LeadingTrivia);
                        leadingTrivia.Add(new SyntaxTrivia(SyntaxKind.EmptyExpandedMacroTrivia, originalToken.Text, originalToken.SourceRange, originalToken.Span, ImmutableArray <Diagnostic> .Empty));
                        leadingTrivia.AddRange(originalToken.TrailingTrivia);
                        leadingTrivia.AddRange(token.LeadingTrivia);
                        token = token.WithLeadingTrivia(leadingTrivia.ToImmutableArray());
                    }
                    else
                    {
                        if (expandedTokens.Count > 1)
                        {
                            _expandedMacroTokens = expandedTokens;
                            _expandedMacroIndex  = 1;
                        }
                        token = expandedTokens[0];
                    }
                }
            }

            return(token);
        }
            private Model FilterModelInBackgroundWorker(
                Model model,
                int id,
                SnapshotPoint caretPosition,
                bool recheckCaretPosition,
                bool dismissIfEmptyAllowed,
                CompletionFilterReason filterReason)
            {
                if (model == null)
                {
                    return null;
                }

                var filterState = model.FilterState;

                // If all the filters are on, or all the filters are off then we don't actually 
                // need to filter.
                if (filterState != null)
                {
                    if (filterState.Values.All(b => b) ||
                        filterState.Values.All(b => !b))
                    {
                        filterState = null;
                    }
                }

                // We want to dismiss the session if the caret ever moved outside our bounds.
                if (recheckCaretPosition && Controller.IsCaretOutsideAllItemBounds(model, caretPosition))
                {
                    return null;
                }

                if (id != _filterId)
                {
                    return model;
                }

                var textSnapshot = caretPosition.Snapshot;
                var allFilteredItems = new List<PresentationItem>();
                var textSpanToText = new Dictionary<TextSpan, string>();
                var helper = this.Controller.GetCompletionHelper();

                // isUnique tracks if there is a single 
                bool? isUnique = null;
                PresentationItem bestFilterMatch = null;
                bool filterTextIsPotentialIdentifier = false;

                var recentItems = this.Controller.GetRecentItems();

                var itemToFilterText = new Dictionary<CompletionItem, string>();
                model = model.WithCompletionItemToFilterText(itemToFilterText);

                foreach (var currentItem in model.TotalItems)
                {
                    // Check if something new has happened and there's a later on filter operation
                    // in the chain.  If so, there's no need for us to do any more work (as it will
                    // just be superceded by the later work).
                    if (id != _filterId)
                    {
                        return model;
                    }

                    // We may have wrapped some items in the list in DescriptionModifying items,
                    // but we should use the actual underlying items when filtering. That way
                    // our rules can access the underlying item's provider.

                    if (ItemIsFilteredOut(currentItem.Item, filterState))
                    {
                        continue;
                    }

                    var filterText = model.GetCurrentTextInSnapshot(currentItem.Item.Span, textSnapshot, textSpanToText);
                    var matchesFilterText = MatchesFilterText(helper, currentItem.Item, filterText, model.Trigger, filterReason, recentItems);
                    itemToFilterText[currentItem.Item] = filterText;

                    if (matchesFilterText)
                    {
                        allFilteredItems.Add(currentItem);

                        // If we have no best match, or this match is better than the last match,
                        // then the current item is the best filter match.
                        if (bestFilterMatch == null ||
                            IsBetterFilterMatch(helper, currentItem.Item, bestFilterMatch.Item, filterText, model.Trigger, filterReason, recentItems))
                        {
                            bestFilterMatch = currentItem;
                        }

                        // If isUnique is null, then this is the first time we've seen an item that
                        // matches the filter text.  That item is now considered unique.  However, if
                        // isUnique is non-null, then this is the second (or third, or fourth, etc.)
                        // that a provider said to include. It's no longer unique.
                        //
                        // Note: We only want to do this if any filter text was actually provided.
                        // This is so we can handle the following cases properly:
                        //
                        //    Console.WriteLi$$
                        //
                        // If they try to commit unique item there, we want to commit to
                        // "WriteLine".  However, if they just have:
                        //
                        //    Console.$$
                        //
                        // And they try to commit unique item, we won't commit something just
                        // because it was in the MRU list.
                        if (filterText != string.Empty)
                        {
                            isUnique = isUnique == null || false;
                        }
                    }
                    else
                    {
                        if (filterText.Length <= 1)
                        {
                            // Even though the rule provider didn't match this, we'll still include it
                            // since we want to allow a user typing a single character and seeing all
                            // possibly completions.  However, we don't consider it either unique or a
                            // filter match, so we won't select it.
                            allFilteredItems.Add(currentItem);
                        }

                        // We want to dismiss the list if the user is typing a # and nothing matches
                        filterTextIsPotentialIdentifier = filterTextIsPotentialIdentifier ||
                            filterText.Length == 0 ||
                            (!char.IsDigit(filterText[0]) && filterText[0] != '-' && filterText[0] != '.');
                    }
                }

                if (!filterTextIsPotentialIdentifier && bestFilterMatch == null)
                {
                    // We had no matches, and the user is typing a #, dismiss the list
                    return null;
                }

                if (allFilteredItems.Count == 0)
                {
                    if (dismissIfEmptyAllowed &&
                        model.DismissIfEmpty &&
                        filterReason != CompletionFilterReason.BackspaceOrDelete)
                    {
                        return null;
                    }

                    if (model.FilterState != null && model.FilterState.Values.Any(b => b))
                    {
                        // If the user has turned on some filtering states, and we filtered down to 
                        // nothing, then we do want the UI to show that to them.
                        return model.WithFilteredItems(allFilteredItems.ToImmutableArray())
                                    .WithHardSelection(false)
                                    .WithIsUnique(false);
                    }
                    else
                    {
                        // If we are going to filter everything out, then just preserve the existing
                        // model, but switch over to soft selection.  Also, nothing is unique at that
                        // point.
                        return model.WithHardSelection(false)
                                    .WithIsUnique(false);
                    }
                }

                // If we have a best item, then select it.  Otherwise just use the first item
                // in the list.
                var selectedItem = bestFilterMatch ?? allFilteredItems.First();

                // If we have a best item, then we want to hard select it.  Otherwise we want
                // soft selection.  However, no hard selection if there's a builder.
                var hardSelection = IsHardSelection(model, bestFilterMatch, textSnapshot, helper, model.Trigger, filterReason);

                var result = model.WithFilteredItems(allFilteredItems.ToImmutableArray())
                                  .WithSelectedItem(selectedItem)
                                  .WithHardSelection(hardSelection)
                                  .WithIsUnique(isUnique.HasValue && isUnique.Value);

                return result;
            }
        public async Task TargetMultipleLogicalPartitionKeys()
        {
            int batchSize = 25;

            string pkToRead1 = "pkToRead1";
            string pkToRead2 = "pkToRead2";
            string otherPK   = "otherPK";

            for (int i = 0; i < batchSize; i++)
            {
                await this.Container.CreateItemAsync(this.CreateRandomToDoActivity(pkToRead1));
            }

            for (int i = 0; i < batchSize; i++)
            {
                await this.Container.CreateItemAsync(this.CreateRandomToDoActivity(pkToRead2));
            }

            for (int i = 0; i < batchSize; i++)
            {
                await this.Container.CreateItemAsync(this.CreateRandomToDoActivity(otherPK));
            }

            // Create one start state for each logical partition key.
            List <FeedRangeState <ReadFeedState> > feedRangeStates = new List <FeedRangeState <ReadFeedState> >();
            IReadOnlyList <string> partitionKeysToTarget           = new List <string>()
            {
                pkToRead1,
                pkToRead2
            };

            foreach (string partitionKeyToTarget in partitionKeysToTarget)
            {
                feedRangeStates.Add(
                    new FeedRangeState <ReadFeedState>(
                        (FeedRangeInternal)FeedRange.FromPartitionKey(
                            new Cosmos.PartitionKey(partitionKeyToTarget)),
                        ReadFeedState.Beginning()));
            }

            // Use the list composition property of the constructor to merge them in to a single state.
            ReadFeedCrossFeedRangeState multipleLogicalPartitionKeyState = new ReadFeedCrossFeedRangeState(feedRangeStates.ToImmutableArray());
            IAsyncEnumerable <TryCatch <ReadFeedPage> > asyncEnumerable  = this.Container.GetReadFeedAsyncEnumerable(multipleLogicalPartitionKeyState);

            (int totalCount, ReadFeedCrossFeedRangeState? _) = await DrainAllAsync(asyncEnumerable);

            Assert.AreEqual(2 * batchSize, totalCount);
        }
Example #55
0
        //TODO move into p2p node
        private static ImmutableArray<UInt256> CalculateBlockLocatorHashes(IImmutableList<ChainedBlock> blockHashes)
        {
            var blockLocatorHashes = new List<UInt256>();

            if (blockHashes.Count > 0)
            {
                var step = 1;
                var start = 0;
                for (var i = blockHashes.Count - 1; i > 0; i -= step, start++)
                {
                    if (start >= 10)
                        step *= 2;

                    blockLocatorHashes.Add(blockHashes[i].BlockHash);
                }
                blockLocatorHashes.Add(blockHashes[0].BlockHash);
            }

            return blockLocatorHashes.ToImmutableArray();
        }
Example #56
0
        private static ImmutableArray <TextChangeRange> Merge(ImmutableArray <TextChangeRange> oldChanges, ImmutableArray <TextChangeRange> newChanges)
        {
            var list = new List <TextChangeRange>(oldChanges.Length + newChanges.Length);

            int oldIndex = 0;
            int newIndex = 0;
            int oldDelta = 0;

nextNewChange:
            if (newIndex < newChanges.Length)
            {
                var newChange = newChanges[newIndex];

nextOldChange:
                if (oldIndex < oldChanges.Length)
                {
                    var oldChange = oldChanges[oldIndex];

tryAgain:
                    if (oldChange.Span.Length == 0 && oldChange.NewLength == 0)
                    {
                        // old change is a non-change, just ignore it and move on
                        oldIndex++;
                        goto nextOldChange;
                    }
                    else if (newChange.Span.Length == 0 && newChange.NewLength == 0)
                    {
                        // new change is a non-change, just ignore it and move on
                        newIndex++;
                        goto nextNewChange;
                    }
                    else if (newChange.Span.End < (oldChange.Span.Start + oldDelta))
                    {
                        // new change occurs entirely before old change
                        var adjustedNewChange = new TextChangeRange(new TextSpan(newChange.Span.Start - oldDelta, newChange.Span.Length), newChange.NewLength);
                        AddRange(list, adjustedNewChange);
                        newIndex++;
                        goto nextNewChange;
                    }
                    else if (newChange.Span.Start > oldChange.Span.Start + oldDelta + oldChange.NewLength)
                    {
                        // new change occurs entirely after old change
                        AddRange(list, oldChange);
                        oldDelta = oldDelta - oldChange.Span.Length + oldChange.NewLength;
                        oldIndex++;
                        goto nextOldChange;
                    }
                    else if (newChange.Span.Start < oldChange.Span.Start + oldDelta)
                    {
                        // new change starts before old change, but overlaps
                        // add as much of new change deletion as possible and try again
                        var newChangeLeadingDeletion = (oldChange.Span.Start + oldDelta) - newChange.Span.Start;
                        AddRange(list, new TextChangeRange(new TextSpan(newChange.Span.Start - oldDelta, newChangeLeadingDeletion), 0));
                        newChange = new TextChangeRange(new TextSpan(oldChange.Span.Start + oldDelta, newChange.Span.Length - newChangeLeadingDeletion), newChange.NewLength);
                        goto tryAgain;
                    }
                    else if (newChange.Span.Start > oldChange.Span.Start + oldDelta)
                    {
                        // new change starts after old change, but overlaps
                        // add as much of the old change as possible and try again
                        var oldChangeLeadingInsertion = newChange.Span.Start - (oldChange.Span.Start + oldDelta);
                        AddRange(list, new TextChangeRange(oldChange.Span, oldChangeLeadingInsertion));
                        oldDelta  = oldDelta - oldChange.Span.Length + oldChangeLeadingInsertion;
                        oldChange = new TextChangeRange(new TextSpan(oldChange.Span.Start, 0), oldChange.NewLength - oldChangeLeadingInsertion);
                        newChange = new TextChangeRange(new TextSpan(oldChange.Span.Start + oldDelta, newChange.Span.Length), newChange.NewLength);
                        goto tryAgain;
                    }
                    else if (newChange.Span.Start == oldChange.Span.Start + oldDelta)
                    {
                        // new change and old change start at same position
                        if (oldChange.NewLength == 0)
                        {
                            // old change is just a deletion, go ahead and old change now and deal with new change separately
                            AddRange(list, oldChange);
                            oldDelta = oldDelta - oldChange.Span.Length + oldChange.NewLength;
                            oldIndex++;
                            goto nextOldChange;
                        }
                        else if (newChange.Span.Length == 0)
                        {
                            // new change is just an insertion, go ahead and tack it on with old change
                            AddRange(list, new TextChangeRange(oldChange.Span, oldChange.NewLength + newChange.NewLength));
                            oldDelta = oldDelta - oldChange.Span.Length + oldChange.NewLength;
                            oldIndex++;
                            newIndex++;
                            goto nextNewChange;
                        }
                        else
                        {
                            // delete as much from old change as new change can
                            // a new change deletion is a reduction in the old change insertion
                            var oldChangeReduction = Math.Min(oldChange.NewLength, newChange.Span.Length);
                            AddRange(list, new TextChangeRange(oldChange.Span, oldChange.NewLength - oldChangeReduction));
                            oldDelta = oldDelta - oldChange.Span.Length + (oldChange.NewLength - oldChangeReduction);
                            oldIndex++;

                            // deduct the amount removed from oldChange from newChange's deletion span (since its already been applied)
                            newChange = new TextChangeRange(new TextSpan(oldChange.Span.Start + oldDelta, newChange.Span.Length - oldChangeReduction), newChange.NewLength);
                            goto nextOldChange;
                        }
                    }
                }
                else
                {
                    // no more old changes, just add adjusted new change
                    var adjustedNewChange = new TextChangeRange(new TextSpan(newChange.Span.Start - oldDelta, newChange.Span.Length), newChange.NewLength);
                    AddRange(list, adjustedNewChange);
                    newIndex++;
                    goto nextNewChange;
                }
            }
            else
            {
                // no more new changes, just add remaining old changes
                while (oldIndex < oldChanges.Length)
                {
                    AddRange(list, oldChanges[oldIndex]);
                    oldIndex++;
                }
            }

            return(list.ToImmutableArray());
        }
Example #57
0
        public virtual IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            var match = OrderList.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                match = UnorderList.Match(context.CurrentMarkdown);
                if (match.Length == 0)
                {
                    return(null);
                }
            }
            var sourceInfo = context.Consume(match.Length);

            var bull = match.Groups[2].Value;

            var cap = match.Groups[0].Value.Match(Item);

            var next       = false;
            var l          = cap.Length;
            int i          = 0;
            var tokens     = new List <IMarkdownToken>();
            var lineOffset = 0;
            var lines      = 0;

            for (; i < l; i++)
            {
                var item = cap[i];
                lines = CountLine(item);
                // Remove the list item's bullet
                // so it is seen as the next token.
                var space = item.Length;
                item = item.ReplaceRegex(Regexes.Lexers.LeadingBullet, string.Empty);

                // Outdent whatever the
                // list item contains. Hacky.
                if (item.IndexOf("\n ") > -1)
                {
                    space -= item.Length;
                    item   = !parser.Options.Pedantic
                      ? Regex.Replace(item, "^ {1," + space + "}", "", RegexOptions.Multiline)
                      : Regex.Replace(item, @"^ {1,4}", "", RegexOptions.Multiline);
                }

                // Determine whether item is loose or not.
                // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
                // for discount behavior.
                var loose = next || Regex.IsMatch(item, @"\n\n(?!\s*$)");
                if (i != l - 1 && item.Length != 0)
                {
                    next = item[item.Length - 1] == '\n';
                    if (!loose)
                    {
                        loose = next;
                    }
                }

                var c = parser.SwitchContext(MarkdownBlockContext.IsTop, false);
                if (!loose)
                {
                    var bc = (MarkdownBlockContext)parser.Context;
                    c = parser.SwitchContext(
                        bc.SetRules(
                            ImmutableList.Create <IMarkdownRule>(
                                this,
                                new MarkdownNewLineBlockRule(),
                                new MarkdownTextBlockRule())));
                }
                var itemSourceInfo = sourceInfo.Copy(item, lineOffset);
                var blockTokens    = parser.Tokenize(itemSourceInfo);
                parser.SwitchContext(c);
                blockTokens = TokenHelper.CreateParagraghs(parser, this, blockTokens, loose, itemSourceInfo);
                tokens.Add(new MarkdownListItemBlockToken(this, parser.Context, blockTokens, loose, itemSourceInfo));
                lineOffset += lines;
            }

            return(new MarkdownListBlockToken(this, parser.Context, tokens.ToImmutableArray(), bull.Length > 1, sourceInfo));
        }
Example #58
0
        internal BatchSignInput GenerateListOfFiles()
        {
            Stopwatch gatherInfoTime = Stopwatch.StartNew();

            foreach (var itemToSign in _itemsToSign)
            {
                string fullPath            = itemToSign.ItemSpec;
                string collisionPriorityId = itemToSign.GetMetadata(SignToolConstants.CollisionPriorityId);
                var    contentHash         = ContentUtil.GetContentHash(fullPath);
                var    fileUniqueKey       = new SignedFileContentKey(contentHash, Path.GetFileName(fullPath));

                if (!_whichPackagesTheFileIsIn.TryGetValue(fileUniqueKey, out var packages))
                {
                    packages = new HashSet <string>();
                }

                packages.Add(fullPath);

                _whichPackagesTheFileIsIn[fileUniqueKey] = packages;

                PathWithHash pathWithHash = new PathWithHash(fullPath, contentHash);
                TrackFile(pathWithHash, null, collisionPriorityId);
            }
            gatherInfoTime.Stop();
            if (_telemetry != null)
            {
                _telemetry.AddMetric("Gather file info duration (s)", gatherInfoTime.ElapsedMilliseconds / 1000);
            }

            if (_errors.Any())
            {
                // Iterate over each pair of <error code, unique file identity>.
                // We can be sure here that the same file won't have the same error code twice.
                foreach (var errorGroup in _errors)
                {
                    switch (errorGroup.Key)
                    {
                    case SigningToolErrorCode.SIGN002:
                        _log.LogError("Could not determine certificate name for signable file(s):");
                        break;
                    }

                    // For each file that had that error
                    foreach (var erroredFile in errorGroup.Value)
                    {
                        _log.LogError($"\tFile: {erroredFile.FileName}");

                        // Get a list of all containers where the file showed up
                        foreach (var containerName in _whichPackagesTheFileIsIn[erroredFile])
                        {
                            _log.LogError($"\t\t{containerName}");
                        }
                    }
                }
            }

            return(new BatchSignInput(_filesToSign.ToImmutableArray(), _zipDataMap.ToImmutableDictionary(), _filesToCopy.ToImmutableArray()));
        }
        private async Task<ImmutableArray<CodeAction>> GetOfferedFixesInternalAsync(string language, string source, int? diagnosticIndex, ImmutableArray<DiagnosticAnalyzer> analyzers, CodeFixProvider codeFixProvider, CancellationToken cancellationToken)
        {
            var document = this.CreateDocument(source, language);
            var analyzerDiagnostics = await GetSortedDiagnosticsFromDocumentsAsync(analyzers, new[] { document }, cancellationToken).ConfigureAwait(false);

            var index = diagnosticIndex.HasValue ? diagnosticIndex.Value : 0;

            Assert.True(index < analyzerDiagnostics.Count());

            var actions = new List<CodeAction>();
            var context = new CodeFixContext(document, analyzerDiagnostics[index], (a, d) => actions.Add(a), cancellationToken);
            await codeFixProvider.RegisterCodeFixesAsync(context).ConfigureAwait(false);

            return actions.ToImmutableArray();
        }
        public async Task <CodeAnalysisResults> RunCodeAnalysisAsync(IEditor editor, List <UnsavedFile> unsavedFiles,
                                                                     Func <bool> interruptRequested)
        {
            var result = new CodeAnalysisResults();

            var dataAssociation = GetAssociatedData(editor.SourceFile);

            var clangUnsavedFiles = new List <ClangUnsavedFile>();

            clangUnsavedFiles.AddRange(unsavedFiles.Select(f => new ClangUnsavedFile(f.FileName, f.Contents)));

            var diagnostics = new List <Diagnostic>();

            await clangAccessJobRunner.InvokeAsync(() =>
            {
                try
                {
                    var translationUnit = GetAndParseTranslationUnit(editor, clangUnsavedFiles);

                    if (translationUnit != null)
                    {
                        if (editor.SourceFile != null && translationUnit != null)
                        {
                            ScanTokens(translationUnit, result.SyntaxHighlightingData);

                            GenerateHighlightData(translationUnit.GetCursor(), result.SyntaxHighlightingData, result.IndexItems);
                        }

                        GenerateDiagnostics(translationUnit.DiagnosticSet.Items, translationUnit, editor.SourceFile, diagnostics);
                    }
                }
                catch (Exception e)
                {
                }
            });

            DiagnosticsUpdated?.Invoke(this, new DiagnosticsUpdatedEventArgs(this, editor.SourceFile, diagnostics.Count > 0 ? DiagnosticsUpdatedKind.DiagnosticsCreated : DiagnosticsUpdatedKind.DiagnosticsRemoved, diagnostics.ToImmutableArray()));

            return(result);
        }