async Task <IDocumentOptions> IDocumentOptionsProvider.GetOptionsForDocumentAsync(Document document, CancellationToken cancellationToken)
        {
            var mdws    = document.Project.Solution.Workspace as MonoDevelopWorkspace;
            var project = mdws?.GetMonoProject(document.Project.Id);

            var types = Ide.DesktopService.GetMimeTypeInheritanceChain(CSharpFormatter.MimeType);

            CSharpFormattingPolicy policy;
            TextStylePolicy        textpolicy;

            if (project == null)
            {
                textpolicy = PolicyService.InvariantPolicies.Get <TextStylePolicy> (types);
                policy     = PolicyService.InvariantPolicies.Get <CSharpFormattingPolicy> (types);
            }
            else
            {
                var policyParent = project.Policies;
                policy     = policyParent.Get <CSharpFormattingPolicy> (types);
                textpolicy = policyParent.Get <TextStylePolicy> (types);
            }
            var path = GetPath(document);
            ICodingConventionContext conventions = null;

            try {
                if (path != null)
                {
                    conventions = await EditorConfigService.GetEditorConfigContext(path, cancellationToken);
                }
            } catch (Exception e) {
                LoggingService.LogError("Error while loading coding conventions.", e);
            }
            return(new DocumentOptions(policy.CreateOptions(textpolicy), conventions?.CurrentConventions));
        }
Пример #2
0
        async Task <IDocumentOptions> IDocumentOptionsProvider.GetOptionsForDocumentAsync(Document document, CancellationToken cancellationToken)
        {
            var mdws    = document.Project.Solution.Workspace as MonoDevelopWorkspace;
            var project = mdws?.GetMonoProject(document.Project.Id);

            var path = GetPath(document);
            ICodingConventionContext conventions = null;

            try {
                if (path != null)
                {
                    conventions = await EditorConfigService.GetEditorConfigContext(path, cancellationToken);
                }
            } catch (Exception e) {
                LoggingService.LogError("Error while loading coding conventions.", e);
            }
            return(new DocumentOptions(project?.Policies, conventions?.CurrentConventions));
        }
Пример #3
0
        protected override ITextSource FormatImplementation(PolicyContainer policyParent, string mimeType, ITextSource input, int startOffset, int length)
        {
            var chain      = IdeServices.DesktopService.GetMimeTypeInheritanceChain(mimeType);
            var policy     = policyParent.Get <CSharpFormattingPolicy> (chain);
            var textPolicy = policyParent.Get <TextStylePolicy> (chain);
            var optionSet  = policy.CreateOptions(textPolicy);

            if (input is IReadonlyTextDocument doc)
            {
                try {
                    var conventions = EditorConfigService.GetEditorConfigContext(doc.FileName).WaitAndGetResult();
                    if (conventions != null)
                    {
                        optionSet = new FormattingDocumentOptionSet(optionSet, new DocumentOptions(optionSet, conventions.CurrentConventions));
                    }
                } catch (Exception e) {
                    LoggingService.LogError("Error while loading coding conventions.", e);
                }
            }

            return(new StringTextSource(FormatText(optionSet, input.Text, startOffset, startOffset + length)));
        }
        // Returns a stream with the content of the file.
        // project and language parameters are optional
        public virtual async Task <Stream> CreateFileContentAsync(SolutionFolderItem policyParent, Project project, string language, string fileName, string identifier)
        {
            var model = CombinedTagModel.GetTagModel(ProjectTagModel, policyParent, project, language, identifier, fileName);

            //HACK: for API compat, CreateContent just gets the override, not the base model
            // but ProcessContent gets the entire model
            string content = CreateContent(project, model.OverrideTags, language);

            content = ProcessContent(content, model);

            string mime      = DesktopService.GetMimeTypeForUri(fileName);
            var    formatter = !string.IsNullOrEmpty(mime) ? CodeFormatterService.GetFormatter(mime) : null;

            if (formatter != null)
            {
                var formatted = formatter.FormatText(policyParent != null ? policyParent.Policies : null, content);
                if (formatted != null)
                {
                    content = formatted;
                }
            }

            var             ms         = new MemoryStream();
            Encoding        encoding   = null;
            TextStylePolicy textPolicy = policyParent != null?policyParent.Policies.Get <TextStylePolicy> (mime ?? "text/plain")
                                             : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <TextStylePolicy> (mime ?? "text/plain");

            string eolMarker = TextStylePolicy.GetEolMarker(textPolicy.EolMarker);

            var ctx = await EditorConfigService.GetEditorConfigContext(fileName);

            if (ctx != null)
            {
                ctx.CurrentConventions.UniversalConventions.TryGetEncoding(out encoding);
                if (ctx.CurrentConventions.UniversalConventions.TryGetLineEnding(out string lineEnding))
                {
                    eolMarker = lineEnding;
                }
            }
            if (encoding == null)
            {
                encoding = System.Text.Encoding.UTF8;
            }
            var bom = encoding.GetPreamble();

            if (bom != null && bom.Length > 0)
            {
                ms.Write(bom, 0, bom.Length);
            }

            byte[] data;
            if (AddStandardHeader)
            {
                string header = StandardHeaderService.GetHeader(policyParent, fileName, true);
                data = encoding.GetBytes(header);
                ms.Write(data, 0, data.Length);
            }

            var doc = TextEditorFactory.CreateNewDocument();

            doc.Text = content;


            byte[] eolMarkerBytes = encoding.GetBytes(eolMarker);

            var           tabToSpaces = textPolicy.TabsToSpaces? new string (' ', textPolicy.TabWidth) : null;
            IDocumentLine lastLine    = null;

            foreach (var line in doc.GetLines())
            {
                var lineText = doc.GetTextAt(line.Offset, line.Length);
                if (tabToSpaces != null)
                {
                    lineText = lineText.Replace("\t", tabToSpaces);
                }
                if (line.LengthIncludingDelimiter > 0)
                {
                    data = encoding.GetBytes(lineText);
                    ms.Write(data, 0, data.Length);
                    ms.Write(eolMarkerBytes, 0, eolMarkerBytes.Length);
                }
                lastLine = line;
            }
            if (ctx != null && lastLine != null && lastLine.Length > 0)
            {
                if (ctx.CurrentConventions.UniversalConventions.TryGetRequireFinalNewline(out bool requireNewLine))
                {
                    if (requireNewLine)
                    {
                        ms.Write(eolMarkerBytes, 0, eolMarkerBytes.Length);
                    }
                }
            }

            ms.Position = 0;
            return(ms);
        }