Example #1
0
        public async Task <TextEdit[]> DocumentOnTypeFormatting(DocumentOnTypeFormattingParams @params, CancellationToken cancellationToken)
        {
            int targetLine;

            switch (@params.ch)
            {
            case "\n":
                targetLine = @params.position.line - 1;
                break;

            case ";":
            case ":":
                targetLine = @params.position.line;
                break;

            default:
                throw new ArgumentException("unexpected trigger character", nameof(@params.ch));
            }

            var uri = @params.textDocument.uri;
            var doc = _rdt.GetDocument(uri);

            if (doc == null)
            {
                return(Array.Empty <TextEdit>());
            }

            using (var reader = new StringReader(doc.Content)) {
                if (@params.ch == ":")
                {
                    return(await BlockFormatter.ProvideEdits(reader, @params.position, @params.options));
                }

                var lineFormatter  = new LineFormatter(reader, doc.Interpreter.LanguageVersion);
                var edits          = lineFormatter.FormatLine(targetLine);
                var unmatchedToken = lineFormatter.UnmatchedToken(targetLine);

                if (unmatchedToken != null)
                {
                    var message = Resources.LineFormatter_UnmatchedToken.FormatInvariant(unmatchedToken.Value.token, unmatchedToken.Value.line + 1);
                    _log?.Log(TraceEventType.Warning, message);
                }

                return(edits);
            }
        }
        public override async Task <TextEdit[]> DocumentOnTypeFormatting(DocumentOnTypeFormattingParams @params, CancellationToken cancellationToken)
        {
            int targetLine;

            switch (@params.ch)
            {
            case "\n":
                targetLine = @params.position.line - 1;
                break;

            case ";":
            case ":":
                targetLine = @params.position.line;
                break;

            default:
                throw new ArgumentException("unexpected trigger character", nameof(@params.ch));
            }

            var uri = @params.textDocument.uri;

            if (!(ProjectFiles.GetEntry(uri) is IDocument doc))
            {
                return(Array.Empty <TextEdit>());
            }
            var part = ProjectFiles.GetPart(uri);

            using (var reader = doc.ReadDocument(part, out _)) {
                if (@params.ch == ":")
                {
                    return(await BlockFormatter.ProvideEdits(reader, @params.position, @params.options));
                }

                var lineFormatter  = new LineFormatter(reader, Analyzer.LanguageVersion);
                var edits          = lineFormatter.FormatLine(targetLine);
                var unmatchedToken = lineFormatter.UnmatchedToken(targetLine);

                if (unmatchedToken != null)
                {
                    var message = Resources.LineFormatter_UnmatchedToken.FormatInvariant(unmatchedToken.Value.token, unmatchedToken.Value.line + 1);
                    LogMessage(MessageType.Warning, message);
                }

                return(edits);
            }
        }
Example #3
0
        public override async Task <TextEdit[]> DocumentOnTypeFormatting(DocumentOnTypeFormattingParams @params, CancellationToken cancellationToken)
        {
            int targetLine;

            switch (@params.ch)
            {
            case "\n":
                targetLine = @params.position.line - 1;
                break;

            case ";":
            case ":":
                targetLine = @params.position.line;
                break;

            default:
                throw new ArgumentException("unexpected trigger character", nameof(@params.ch));
            }

            var uri = @params.textDocument.uri;

            if (!(ProjectFiles.GetEntry(uri) is IDocument doc))
            {
                return(Array.Empty <TextEdit>());
            }
            var part = ProjectFiles.GetPart(uri);

            using (var reader = doc.ReadDocument(part, out _)) {
                if (@params.ch == ":")
                {
                    return(await BlockFormatter.ProvideEdits(reader, @params.position, @params.options));
                }

                var lineFormatter = new LineFormatter(reader, Analyzer.LanguageVersion);
                return(lineFormatter.FormatLine(targetLine));
            }
        }