Ejemplo n.º 1
0
        public async Task <TextEditContainer> Handle(DocumentFormattingParams request, CancellationToken cancellationToken)
        {
            var scriptFile   = _workspaceService.GetFile(request.TextDocument.Uri);
            var pssaSettings = _configurationService.CurrentSettings.CodeFormatting.GetPSSASettingsHashtable(
                (int)request.Options.TabSize,
                request.Options.InsertSpaces,
                _logger);


            // TODO raise an error event in case format returns null
            string formattedScript;
            Range  editRange;
            var    extent = scriptFile.ScriptAst.Extent;

            // todo create an extension for converting range to script extent
            editRange = new Range
            {
                Start = new Position
                {
                    Line      = extent.StartLineNumber - 1,
                    Character = extent.StartColumnNumber - 1
                },
                End = new Position
                {
                    Line      = extent.EndLineNumber - 1,
                    Character = extent.EndColumnNumber - 1
                }
            };

            formattedScript = await _analysisService.FormatAsync(
                scriptFile.Contents,
                pssaSettings,
                null).ConfigureAwait(false);

            formattedScript = formattedScript ?? scriptFile.Contents;

            return(new TextEditContainer(new TextEdit
            {
                NewText = formattedScript,
                Range = editRange
            }));
        }