Esempio n. 1
0
        public static string FormatText(Microsoft.CodeAnalysis.Options.OptionSet optionSet, string input, int startOffset, int endOffset)
        {
            var inputTree = CSharpSyntaxTree.ParseText(input);

            var root   = inputTree.GetRoot();
            var doc    = Formatter.Format(root, new TextSpan(startOffset, endOffset - startOffset), TypeSystemService.Workspace, optionSet);
            var result = doc.ToFullString();

            return(result.Substring(startOffset, endOffset + result.Length - input.Length - startOffset));
        }
Esempio n. 2
0
        protected override async void CorrectIndentingImplementation(PolicyContainer policyParent, TextEditor editor, int line)
        {
            var lineSegment = editor.GetLine(line);

            if (lineSegment == null)
            {
                return;
            }

            try {
                Microsoft.CodeAnalysis.Options.OptionSet options = null;

                foreach (var doc in IdeApp.Workbench.Documents)
                {
                    if (doc.Editor == editor)
                    {
                        options = await doc.AnalysisDocument?.GetOptionsAsync();

                        break;
                    }
                }

                if (options == null)
                {
                    var policy     = policyParent.Get <CSharpFormattingPolicy> (MimeType);
                    var textpolicy = policyParent.Get <TextStylePolicy> (MimeType);
                    options = policy.CreateOptions(textpolicy);
                }

                var tracker = new CSharpIndentEngine(options);

                tracker.Update(IdeApp.Workbench.ActiveDocument.Editor, lineSegment.Offset);
                for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.Length; i++)
                {
                    tracker.Push(editor.GetCharAt(i));
                }

                string curIndent = lineSegment.GetIndentation(editor);

                int nlwsp = curIndent.Length;
                if (!tracker.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.LengthIncludingDelimiter && editor.GetCharAt(lineSegment.Offset + nlwsp) == '*'))
                {
                    // Possibly replace the indent
                    string newIndent = tracker.ThisLineIndent;
                    if (newIndent != curIndent)
                    {
                        editor.ReplaceText(lineSegment.Offset, nlwsp, newIndent);
                    }
                }
            } catch (Exception e) {
                LoggingService.LogError("Error while indenting", e);
            }
        }
Esempio n. 3
0
        private async Task <Solution> MakeUppercaseAsync(Document document, TypeDeclarationSyntax typeDecl, CancellationToken cancellationToken)
        {
            // Compute new uppercase name.
            SyntaxToken identifierToken = typeDecl.Identifier;
            string      newName         = identifierToken.Text.ToUpperInvariant();

            // Get the symbol representing the type to be renamed.
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken);

            INamedTypeSymbol typeSymbol = semanticModel.GetDeclaredSymbol(typeDecl, cancellationToken);

            // Produce a new solution that has all references to that type renamed, including the declaration.
            Solution originalSolution = document.Project.Solution;

            Microsoft.CodeAnalysis.Options.OptionSet optionSet = originalSolution.Workspace.Options;
            Solution newSolution = await Renamer.RenameSymbolAsync(document.Project.Solution, typeSymbol, newName, optionSet, cancellationToken).ConfigureAwait(false);

            // Return the new solution with the now-uppercase type name.
            return(newSolution);
        }
Esempio n. 4
0
 public override bool ShouldTriggerCompletion(SourceText text, int position, CompletionTrigger trigger, Microsoft.CodeAnalysis.Options.OptionSet options)
 {
     return(trigger.Character == '(' || trigger.Character == '[' || trigger.Character == ',' || base.ShouldTriggerCompletion(text, position, trigger, options));
 }
        public override bool ShouldTriggerCompletion(SourceText text, int position, CompletionTrigger trigger, Microsoft.CodeAnalysis.Options.OptionSet options)
        {
            // This method is overridden because Roslyn's Completion Service calls us, in cases
            // where the change kind is an Insertion or Deletion. This mimics the behavior
            // of the OverrideCompletionProvider provided by Roslyn, so we'll now get called
            // in all the same cases as that one. The goal is to also display our protocol member
            // completion suggestions, even when the user types "overrides" and presses space.
            // See here: http://source.roslyn.io/#Microsoft.CodeAnalysis.Features/Completion/CompletionServiceWithProviders.cs,231
            switch (trigger.Kind)
            {
            case CompletionTriggerKind.Insertion when position > 0:
                var insertedCharacterPosition = position - 1;
                return(Microsoft.CodeAnalysis.CSharp.Completion.Providers.CompletionUtilities.IsTriggerAfterSpaceOrStartOfWordCharacter(text, insertedCharacterPosition, options));

            default:
                return(false);
            }
        }
        internal override bool IsInsertionTrigger(SourceText text, int insertedCharacterPosition, Microsoft.CodeAnalysis.Options.OptionSet options)
        {
            // Bring up on space or at the start of a word, or after a ( or [.
            //
            // Note: we don't want to bring this up after traditional enum operators like & or |.
            // That's because we don't like the experience where the enum appears directly after the
            // operator.  Instead, the user normally types <space> and we will bring up the list
            // then.
            var ch = text [insertedCharacterPosition];

            return
                (ch == ' ' ||
                 ch == '[' ||
                 ch == '(' ||
                 (CompletionUtilities.IsStartingNewWord(text, insertedCharacterPosition)));
        }
 public override bool ShouldTriggerCompletion(SourceText text, int position, CompletionTrigger trigger, Microsoft.CodeAnalysis.Options.OptionSet options)
 {
     // Only commit on dot.
     return(trigger.Character == '.');
 }