public static Edit[] GetEditsAfterKeystroke(string code, int position, char ch, FormattingOptions options = null) {
     using (new DebugTimer("FormatKeyStroke")) {
         if (ch == ';' || ch == '}') {
             var ast = new JSParser(code).Parse(new CodeSettings() { AllowShebangLine = true });
             var visitor = new RangeVisitor(ch, position, ast);
             ast.Walk(visitor);
             if (visitor.Span != default(IndexSpan)) {
                 return FilterRange(
                     visitor.Span.Start,
                     visitor.Span.End,
                     GetEdits(code, options, ast)
                 );
             }
         }
         return new Edit[0];
     }
 }
Exemplo n.º 2
0
        internal static TextSpan?GetDataTipSpan(IWpfTextView wpfTextView, TextSpan selection)
        {
            // Adjust the span to expression boundaries.
            var snapshot = wpfTextView.TextSnapshot;
            var start    = LineAndColumnNumberToSnapshotPoint(snapshot, selection.iStartLine, selection.iStartIndex);
            var end      = LineAndColumnNumberToSnapshotPoint(snapshot, selection.iEndLine, selection.iEndIndex);

            // If this is a zero-length span (which it usually is, unless there's selection), adjust it
            // to cover one char to the right, since an empty span at the beginning of the expression does
            // not count as belonging to that expression;
            if (start == end && start.Position != snapshot.Length)
            {
                end += 1;
            }

            var snapshotSpan = new SnapshotSpan(start, end);
            var trackingSpan = snapshot.CreateTrackingSpan(snapshotSpan.Span, SpanTrackingMode.EdgeExclusive);
            var rep          = new ReverseExpressionParser(snapshot, wpfTextView.TextBuffer, trackingSpan);
            var exprSpan     = rep.GetExpressionRange(forCompletion: false);

            if (exprSpan == null)
            {
                return(null);
            }

            // Check whether this is an expression with side effects - if it does, we don't want to show a data tip for it.
            string text = exprSpan.Value.GetText();
            var    ast  = new JSParser(text).Parse(new CodeSettings());

            var sideEffectsDetectingVisitor = new SideEffectsDetectingVisitor();

            ast.Walk(sideEffectsDetectingVisitor);
            if (sideEffectsDetectingVisitor.HasSideEffects)
            {
                return(null);
            }

            TextSpan dataTipSpan;

            SnapshotPointToLineAndColumnNumber(exprSpan.Value.Start, out dataTipSpan.iStartLine, out dataTipSpan.iStartIndex);
            SnapshotPointToLineAndColumnNumber(exprSpan.Value.End, out dataTipSpan.iEndLine, out dataTipSpan.iEndIndex);
            return(dataTipSpan);
        }
Exemplo n.º 3
0
 public static Edit[] GetEditsAfterKeystroke(string code, int position, char ch, FormattingOptions options = null)
 {
     using (new DebugTimer("FormatKeyStroke")) {
         if (ch == ';' || ch == '}')
         {
             var ast = new JSParser(code).Parse(new CodeSettings()
             {
                 AllowShebangLine = true
             });
             var visitor = new RangeVisitor(ch, position, ast);
             ast.Walk(visitor);
             if (visitor.Span != default(IndexSpan))
             {
                 return(FilterRange(
                            visitor.Span.Start,
                            visitor.Span.End,
                            GetEdits(code, options, ast)
                            ));
             }
         }
         return(new Edit[0]);
     }
 }
        internal static TextSpan? GetDataTipSpan(IWpfTextView wpfTextView, TextSpan selection) {
            // Adjust the span to expression boundaries.
            var snapshot = wpfTextView.TextSnapshot;
            var start = LineAndColumnNumberToSnapshotPoint(snapshot, selection.iStartLine, selection.iStartIndex);
            var end = LineAndColumnNumberToSnapshotPoint(snapshot, selection.iEndLine, selection.iEndIndex);

            // If this is a zero-length span (which it usually is, unless there's selection), adjust it
            // to cover one char to the right, since an empty span at the beginning of the expression does
            // not count as belonging to that expression;
            if (start == end && start.Position != snapshot.Length) {
                end += 1;
            }

            var snapshotSpan = new SnapshotSpan(start, end);
            var trackingSpan = snapshot.CreateTrackingSpan(snapshotSpan.Span, SpanTrackingMode.EdgeExclusive);
            var rep = new ReverseExpressionParser(snapshot, wpfTextView.TextBuffer, trackingSpan);
            var exprSpan = rep.GetExpressionRange(forCompletion: false);
            if (exprSpan == null) {
                return null;
            }

            // Check whether this is an expression with side effects - if it does, we don't want to show a data tip for it.
            string text = exprSpan.Value.GetText();
            var ast = new JSParser(text).Parse(new CodeSettings());

            var sideEffectsDetectingVisitor = new SideEffectsDetectingVisitor();
            ast.Walk(sideEffectsDetectingVisitor);
            if (sideEffectsDetectingVisitor.HasSideEffects) {
                return null;
            }

            TextSpan dataTipSpan;
            SnapshotPointToLineAndColumnNumber(exprSpan.Value.Start, out dataTipSpan.iStartLine, out dataTipSpan.iStartIndex);
            SnapshotPointToLineAndColumnNumber(exprSpan.Value.End, out dataTipSpan.iEndLine, out dataTipSpan.iEndIndex);
            return dataTipSpan;
        }