Ejemplo n.º 1
0
        static SyntaxHighlightingDefinition GetSyntaxHighlightingDefinitionByMimeType(string mimeType)
        {
            foreach (string mt in DesktopService.GetMimeTypeInheritanceChain(mimeType))
            {
                if (mimeType == "application/octet-stream" || mimeType == "text/plain")
                {
                    return(null);
                }

                foreach (var bundle in languageBundles)
                {
                    foreach (var h in bundle.Highlightings)
                    {
                        foreach (var fe in h.FileTypes)
                        {
                            var uri  = fe.StartsWith(".", StringComparison.Ordinal) ? "a" + fe : "a." + fe;
                            var mime = DesktopService.GetMimeTypeForUri(uri);
                            if (mimeType == mime)
                            {
                                return(h);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        void HandlePolicyChanged(object sender, MonoDevelop.Projects.Policies.PolicyChangedEventArgs args)
        {
            var mimeTypes     = DesktopService.GetMimeTypeInheritanceChain(textEditor.MimeType);
            var currentPolicy = policyContainer.Get <TextStylePolicy> (mimeTypes);

            textEditor.Options = DefaultSourceEditorOptions.Instance.WithTextStyle(currentPolicy);
        }
Ejemplo n.º 3
0
        public static void Format(TextEditor editor, DocumentContext context, int startOffset, int endOffset, bool exact = true, OptionSet optionSet = null)
        {
            var policyParent  = context.Project != null ? context.Project.Policies : PolicyService.DefaultPolicies;
            var mimeTypeChain = DesktopService.GetMimeTypeInheritanceChain(CSharpFormatter.MimeType);

            Format(policyParent, mimeTypeChain, editor, context, startOffset, endOffset, exact, optionSet: optionSet);
        }
Ejemplo n.º 4
0
        public static void Format(MonoDevelop.Ide.Gui.Document data, int startOffset, int endOffset, bool exact = true)
        {
            var policyParent  = data.Project != null ? data.Project.Policies : PolicyService.DefaultPolicies;
            var mimeTypeChain = DesktopService.GetMimeTypeInheritanceChain(CSharpFormatter.MimeType);

            Format(policyParent, mimeTypeChain, data, startOffset, endOffset, exact);
        }
        public void UpdateStyleParent(Project styleParent, string mimeType)
        {
            if (styleParent != null && policyContainer == styleParent.Policies && mimeType == lastMimeType)
            {
                return;
            }
            lastMimeType = mimeType;

            if (policyContainer != null)
            {
                policyContainer.PolicyChanged -= HandlePolicyChanged;
            }

            if (string.IsNullOrEmpty(mimeType))
            {
                mimeType = "text/plain";
            }
            this.mimeTypes = DesktopService.GetMimeTypeInheritanceChain(mimeType);

            if (styleParent != null)
            {
                policyContainer = styleParent.Policies;
            }
            else
            {
                policyContainer = MonoDevelop.Projects.Policies.PolicyService.DefaultPolicies;
            }
            currentPolicy = policyContainer.Get <TextStylePolicy> (mimeTypes);

            policyContainer.PolicyChanged += HandlePolicyChanged;
            if (changed != null)
            {
                this.changed(this, EventArgs.Empty);
            }
        }
Ejemplo n.º 6
0
        MimeTypePanelData AddPanel(string mt)
        {
            var chain = new List <string> (DesktopService.GetMimeTypeInheritanceChain(mt).Where(x => mimeTypesWithPolicies.Contains(x)));

            if (chain.Count == 0)
            {
                return(null);
            }
            MimeTypePanelData    data = new MimeTypePanelData();
            OptionsDialogSection sec  = new MimetypeOptionsDialogSection(mt);

            sec.Fill             = true;
            data.Section         = sec;
            data.MimeType        = mt;
            data.TypeDescription = DesktopService.GetMimeTypeDescription(mt);
            if (string.IsNullOrEmpty(data.TypeDescription))
            {
                data.TypeDescription = mt;
            }
            data.DataObject      = DataObject;
            data.PolicyContainer = policyContainer;
            sec.Label            = data.TypeDescription;
            LoadPolicyTypeData(data, mt, chain);
            typeSections [mt] = data;
            ParentDialog.AddChildSection(this, sec, data);
            return(data);
        }
        async Task UpdateStyleParent(MonoDevelop.Projects.Project styleParent, string mimeType)
        {
            RemovePolicyChangeHandler();

            if (string.IsNullOrEmpty(mimeType))
            {
                mimeType = "text/plain";
            }

            var mimeTypes = DesktopService.GetMimeTypeInheritanceChain(mimeType);

            if (styleParent != null)
            {
                policyContainer = styleParent.Policies;
            }
            else
            {
                policyContainer = MonoDevelop.Projects.Policies.PolicyService.DefaultPolicies;
            }
            var currentPolicy = policyContainer.Get <TextStylePolicy> (mimeTypes);

            policyContainer.PolicyChanged += HandlePolicyChanged;
            ((DefaultSourceEditorOptions)textEditor.Options).UpdateStylePolicy(currentPolicy);

            var context = await EditorConfigService.GetEditorConfigContext(textEditor.FileName, default(CancellationToken));

            if (context == null)
            {
                return;
            }
            ((DefaultSourceEditorOptions)textEditor.Options).SetContext(context);
        }
Ejemplo n.º 8
0
        public static void Format(MonoDevelop.Ide.Gui.Document data, ProjectDom dom, DomLocation location, bool correctBlankLines, bool runAferCR = false)
        {
            PolicyContainer policyParent  = dom != null && dom.Project != null ? dom.Project.Policies  : PolicyService.DefaultPolicies;
            var             mimeTypeChain = DesktopService.GetMimeTypeInheritanceChain(CSharpFormatter.MimeType);

            Format(policyParent, mimeTypeChain, data, dom, location, correctBlankLines, runAferCR);
        }
 void IMimeTypePolicyOptionsPanel.InitializePolicy(PolicyContainer policyContainer, string mimeType, bool isExactMimeType)
 {
     this.mimeType        = mimeType;
     this.policyContainer = policyContainer;
     this.isExactMimeType = isExactMimeType;
     mimeTypeScopes       = DesktopService.GetMimeTypeInheritanceChain(mimeType);
 }
Ejemplo n.º 10
0
        public List <ReferenceSegment> Decompile(TextEditorData data, ITreeNavigator navigator, bool publicOnly)
        {
            if (DomMethodNodeBuilder.HandleSourceCodeEntity(navigator, data))
            {
                return(null);
            }
            var type = CecilLoader.GetCecilObject((IUnresolvedTypeDefinition)navigator.DataItem);

            if (type == null)
            {
                return(null);
            }
            var types      = DesktopService.GetMimeTypeInheritanceChain(data.Document.MimeType);
            var codePolicy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy> (types);
            var settings   = new DecompilerSettings()
            {
                AnonymousMethods        = true,
                AutomaticEvents         = true,
                AutomaticProperties     = true,
                ForEachStatement        = true,
                LockStatement           = true,
                CSharpFormattingOptions = codePolicy.CreateOptions(),
                HideNonPublicMembers    = publicOnly
            };

            return(DomMethodNodeBuilder.Decompile(data, DomMethodNodeBuilder.GetModule(navigator), type, builder => {
                builder.AddType(type);
            }, settings));
        }
Ejemplo n.º 11
0
        public static List <ReferenceSegment> Decompile(TextEditorData data, ModuleDefinition module, TypeDefinition currentType, Action <AstBuilder> setData)
        {
            try {
                var types      = DesktopService.GetMimeTypeInheritanceChain(data.Document.MimeType);
                var codePolicy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy> (types);

                var context = new DecompilerContext(module);
                var source  = new CancellationTokenSource();

                context.CancellationToken = source.Token;
                context.CurrentType       = currentType;

                context.Settings = new DecompilerSettings()
                {
                    AnonymousMethods    = true,
                    AutomaticEvents     = true,
                    AutomaticProperties = true,
                    ForEachStatement    = true,
                    LockStatement       = true
                };

                AstBuilder astBuilder = new AstBuilder(context);

                setData(astBuilder);

                astBuilder.RunTransformations(o => false);
                var output = new ColoredCSharpFormatter(data.Document);
                astBuilder.GenerateCode(output, codePolicy.CreateOptions());
                output.SetDocumentData();
                return(output.ReferencedSegments);
            } catch (Exception e) {
                data.Text = "Decompilation failed: \n" + e;
            }
            return(null);
        }
Ejemplo n.º 12
0
        public static DecompilerSettings GetDecompilerSettings(TextEditor data, bool publicOnly = false)
        {
            var types      = DesktopService.GetMimeTypeInheritanceChain(data.MimeType);
            var codePolicy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy> (types);
            var settings   = DomTypeNodeBuilder.CreateDecompilerSettings(publicOnly, codePolicy);

            return(settings);
        }
        public static List <ReferenceSegment> Decompile(TextEditorData data, ModuleDefinition module, TypeDefinition currentType, Action <AstBuilder> setData)
        {
            var types      = DesktopService.GetMimeTypeInheritanceChain(data.Document.MimeType);
            var codePolicy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy> (types);
            var settings   = DomTypeNodeBuilder.CreateDecompilerSettings(false, codePolicy);

            return(Decompile(data, module, currentType, setData, settings));
        }
Ejemplo n.º 14
0
        public static void FormatStatmentAt(MonoDevelop.Ide.Gui.Document data, DocumentLocation location)
        {
            var offset        = data.Editor.LocationToOffset(location);
            var policyParent  = data.Project != null ? data.Project.Policies : PolicyService.DefaultPolicies;
            var mimeTypeChain = DesktopService.GetMimeTypeInheritanceChain(CSharpFormatter.MimeType);

            Format(policyParent, mimeTypeChain, data, offset, offset, false, true);
        }
Ejemplo n.º 15
0
        protected override ITextSource FormatImplementation(PolicyContainer policyParent, string mimeType, ITextSource input, int startOffset, int length)
        {
            var chain      = DesktopService.GetMimeTypeInheritanceChain(mimeType);
            var policy     = policyParent.Get <CSharpFormattingPolicy> (chain);
            var textPolicy = policyParent.Get <TextStylePolicy> (chain);

            return(new StringTextSource(FormatText(policy.CreateOptions(textPolicy), input.Text, startOffset, startOffset + length)));
        }
Ejemplo n.º 16
0
        public static void FormatStatmentAt(TextEditor editor, DocumentContext context, MonoDevelop.Ide.Editor.DocumentLocation location, OptionSet optionSet = null)
        {
            var offset        = editor.LocationToOffset(location);
            var policyParent  = context.Project != null ? context.Project.Policies : PolicyService.DefaultPolicies;
            var mimeTypeChain = DesktopService.GetMimeTypeInheritanceChain(CSharpFormatter.MimeType);

            Format(policyParent, mimeTypeChain, editor, context, offset, offset, false, true, optionSet: optionSet);
        }
Ejemplo n.º 17
0
        public static string SaveFormattedXml(PolicyContainer policies, AddinDescription adesc)
        {
            XmlDocument doc = adesc.SaveToXml();

            TextStylePolicy     textPolicy = policies.Get <TextStylePolicy> (DesktopService.GetMimeTypeInheritanceChain("application/x-addin+xml"));
            XmlFormattingPolicy xmlPolicy  = policies.Get <XmlFormattingPolicy> (DesktopService.GetMimeTypeInheritanceChain("application/x-addin+xml"));

            return(XmlFormatter.FormatXml(textPolicy, xmlPolicy, doc.OuterXml));
        }
 public static bool IsMimeTypeHandled(string mimeType)
 {
     foreach (var m in DesktopService.GetMimeTypeInheritanceChain(mimeType))
     {
         if (m == TextXmlMimeType || m == ApplicationXmlMimeType)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 19
0
 internal static TypeSystemParserNode GetTypeSystemParserNode(string mimeType, string buildAction)
 {
     foreach (var mt in DesktopService.GetMimeTypeInheritanceChain(mimeType))
     {
         var provider = Parsers.FirstOrDefault(p => p.CanParse(mt, buildAction));
         if (provider != null)
         {
             return(provider);
         }
     }
     return(null);
 }
Ejemplo n.º 20
0
 public static IFoldingParser GetFoldingParser(string mimeType)
 {
     foreach (var mt in DesktopService.GetMimeTypeInheritanceChain(mimeType))
     {
         var node = FoldingParsers.FirstOrDefault(n => n.MimeType == mt);
         if (node != null)
         {
             return(node.CreateInstance() as IFoldingParser);
         }
     }
     return(null);
 }
Ejemplo n.º 21
0
        protected override Core.Text.ITextSource FormatImplementation(PolicyContainer policyParent, string mimeType, Core.Text.ITextSource input, int startOffset, int length)
        {
            if (policyParent == null)
            {
                policyParent = PolicyService.DefaultPolicies;
            }
            var mimeTypeInheritanceChain = DesktopService.GetMimeTypeInheritanceChain(mimeType).ToList();
            var txtPol = policyParent.Get <TextStylePolicy> (mimeTypeInheritanceChain);
            var xmlPol = policyParent.Get <XmlFormattingPolicy> (mimeTypeInheritanceChain);

            return(new StringTextSource(FormatXml(txtPol, xmlPol, input.Text)));
        }
Ejemplo n.º 22
0
        //returns all valid extensions in order of most to least specific
        static IEnumerable <TextEditorExtensionNode> GetEditorExtensions(TextEditor editor, string extensionPoint)
        {
            var returned = new HashSet <TextEditorExtensionNode> ();

            //get the nodes from the extensioncontext rather than the addinmanager
            //so that custom conditions supported by the editor are respected
            var nodes = editor.ExtensionContext.GetExtensionNodes <TextEditorExtensionNode> (extensionPoint);

            //file extensions are cheaper to check than mimetypes, check them first
            //this means exact file extensions take precedence over mimetypes regardless
            //of node ordering but it's not a big deal
            var extension = string.IsNullOrEmpty(editor.FileName) ? null : Path.GetExtension(editor.FileName);

            foreach (var node in nodes)
            {
                if (MatchAny(extension, node.FileExtensions))
                {
                    returned.Add(node);
                    yield return(node);
                }
            }

            //check mimetypes, from most to least specific
            var mimeChain = DesktopService.GetMimeTypeInheritanceChain(editor.MimeType);

            foreach (var mime in mimeChain)
            {
                foreach (var node in nodes)
                {
                    if (!returned.Contains(node) && MatchAny(mime, node.MimeTypes))
                    {
                        returned.Add(node);
                        yield return(node);
                    }
                }
            }

            //finally, return any remaining nodes that don't have restrictions at all
            foreach (var node in nodes)
            {
                if (!returned.Contains(node) && NullOrEmpty(node.MimeTypes) && NullOrEmpty(node.FileExtensions))
                {
                    yield return(node);
                }
            }

            bool MatchAny(string value, string [] matches) =>
            matches != null && matches.Length > 0 &&
            matches.Any(m => string.Equals(m, value, StringComparison.OrdinalIgnoreCase));

            bool NullOrEmpty(string [] arr) => arr == null || arr.Length == 0;
        }
Ejemplo n.º 23
0
        protected override string InternalFormat(PolicyContainer policyParent, string mimeType, string input, int startOffset, int endOffset)
        {
            IEnumerable <string> mtypes        = DesktopService.GetMimeTypeInheritanceChain(mimeType);
            TextStylePolicy      currentPolicy = policyParent != null
                                        ? policyParent.Get <TextStylePolicy> (mtypes)
                                        : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <TextStylePolicy> (mtypes);

            input = input ?? "";
            int           line = 0, col = 0;
            string        eolMarker = currentPolicy.GetEolMarker();
            StringBuilder result    = new StringBuilder();

            for (int i = startOffset; i <= endOffset; i++)
            {
                char ch = input[i];
                switch (ch)
                {
                case '\t':
                    if (currentPolicy.TabsToSpaces)
                    {
                        int tabWidth = GetNextTabstop(col, currentPolicy.TabWidth) - col;
                        result.Append(new string (' ', tabWidth));
                        col += tabWidth;
                    }
                    else
                    {
                        goto default;
                    }
                    break;

                case '\r':
                    if (i + 1 < input.Length && input[i + 1] == '\n')
                    {
                        i++;
                    }
                    goto case '\n';

                case '\n':
                    result.Append(eolMarker);
                    line++;
                    col = 0;
                    break;

                default:
                    result.Append(ch);
                    col++;
                    break;
                }
            }
            return(result.ToString());
        }
Ejemplo n.º 24
0
 static bool IsMimeTypeHandled(string mimeType)
 {
     foreach (var m in DesktopService.GetMimeTypeInheritanceChain(mimeType))
     {
         if (m == TextXmlMimeType || m == ApplicationXmlMimeType)
         {
             return(true);
         }
         if (m == MonoDevelop.XmlEditor.MSBuild.MSBuildTextEditorExtension.MSBuildMimeType)
         {
             return(false);
         }
     }
     return(false);
 }
        public string OutputNode(ProjectDom dom, AstNode node, string indent)
        {
            StringWriter           w            = new StringWriter();
            var                    policyParent = dom != null && dom.Project != null ? dom.Project.Policies : null;
            IEnumerable <string>   types        = DesktopService.GetMimeTypeInheritanceChain(CSharpFormatter.MimeType);
            CSharpFormattingPolicy codePolicy   = policyParent != null?policyParent.Get <CSharpFormattingPolicy> (types) : MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <CSharpFormattingPolicy> (types);

            var formatter = new TextWriterOutputFormatter(w);
            int col       = GetColumn(indent, 0, 4);

            formatter.Indentation = System.Math.Max(0, col / 4);
            OutputVisitor visitor = new OutputVisitor(formatter, codePolicy.CreateOptions());

            node.AcceptVisitor(visitor, null);
            return(w.ToString());
        }
Ejemplo n.º 26
0
		public void UpdateStyleParent (Project styleParent, string mimeType)
		{
			if (policyContainer != null)
				policyContainer.PolicyChanged -= HandlePolicyChanged;

			if (string.IsNullOrEmpty (mimeType))
				mimeType = "text/plain";
			this.mimeTypes = DesktopService.GetMimeTypeInheritanceChain (mimeType);

			if (styleParent != null)
				policyContainer = styleParent.Policies;
			else
				policyContainer = MonoDevelop.Projects.Policies.PolicyService.DefaultPolicies;

			currentPolicy = policyContainer.Get<TextStylePolicy> (mimeTypes);
			policyContainer.PolicyChanged += HandlePolicyChanged;
		}
Ejemplo n.º 27
0
        public static List <ReferenceSegment> Decompile(TextEditorData data, ModuleDefinition module, TypeDefinition currentType, Action <AstBuilder> setData)
        {
            var types      = DesktopService.GetMimeTypeInheritanceChain(data.Document.MimeType);
            var codePolicy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy> (types);
            DecompilerSettings settings = new DecompilerSettings()
            {
                AnonymousMethods        = true,
                AutomaticEvents         = true,
                AutomaticProperties     = true,
                ForEachStatement        = true,
                LockStatement           = true,
                ShowXmlDocumentation    = true,
                CSharpFormattingOptions = codePolicy.CreateOptions()
            };

            return(Decompile(data, module, currentType, setData, settings));
        }
Ejemplo n.º 28
0
        public static string[] GetCommentTags(string fileName)
        {
            //Document doc = IdeApp.Workbench.ActiveDocument;
            string loadedMimeType = DesktopService.GetMimeTypeForUri(fileName);

            SyntaxMode mode = null;

            foreach (string mt in DesktopService.GetMimeTypeInheritanceChain(loadedMimeType))
            {
                mode = SyntaxModeService.GetSyntaxMode(mt);
                if (mode != null)
                {
                    break;
                }
            }

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

            List <string> ctags;

            if (mode.Properties.TryGetValue("LineComment", out ctags) && ctags.Count > 0)
            {
                return(new string [] { ctags [0] });
            }
            List <string> tags = new List <string> ();

            if (mode.Properties.TryGetValue("BlockCommentStart", out ctags))
            {
                tags.Add(ctags [0]);
            }
            if (mode.Properties.TryGetValue("BlockCommentEnd", out ctags))
            {
                tags.Add(ctags [0]);
            }
            if (tags.Count == 2)
            {
                return(tags.ToArray());
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 29
0
        protected override void Initialize()
        {
            base.Initialize();

            // This extension needs to be turned off if the webtooling addin json extension is present.
            //   That addin defines a "text/x-json" mimeType that has multiple levels of inheritance.
            var mimeChain = DesktopService.GetMimeTypeInheritanceChain("text/x-json").ToList();

            jsonExtensionInstalled = (mimeChain.Count > 2);

            if (!jsonExtensionInstalled)
            {
                IStateMachineIndentEngine indentEngine;
                indentEngine = new JSonIndentEngine(Editor, DocumentContext);
                stateTracker = new CacheIndentEngine(indentEngine);
                Editor.IndentationTracker = new JSonIndentationTracker(Editor, stateTracker);
            }
        }
        void UpdateExample(string example)
        {
            string text;

            if (!string.IsNullOrEmpty(example))
            {
                text = Environment.NewLine != "\n" ? example.Replace("\n", Environment.NewLine) : example;
            }
            else
            {
                text = "";
            }

            var types      = DesktopService.GetMimeTypeInheritanceChain(MonoDevelop.CSharp.Formatting.CSharpFormatter.MimeType);
            var textPolicy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <TextStylePolicy> (types);

            texteditor.Text = CSharpFormatter.FormatText(profile, textPolicy, text, 0, text.Length);
        }