public override bool HandleKeyPress(SharpDevelopTextAreaControl editor, char ch)
        {
            if (this.CompletionPossible(editor, ch))
            {
                ResourceResolveResult result = ResourceResolverService.Resolve(editor, ch);
                if (result != null)
                {
                    IResourceFileContent content;
                    if ((content = result.ResourceFileContent) != null)
                    {
                        // If the resolved resource set is the local ICSharpCode.Core resource set
                        // (this may happen through the ICSharpCodeCoreNRefactoryResourceResolver),
                        // we will have to merge in the host resource set (if available)
                        // for the code completion window.
                        if (result.ResourceSetReference.ResourceSetName == ICSharpCodeCoreResourceResolver.ICSharpCodeCoreLocalResourceSetName)
                        {
                            IResourceFileContent hostContent = ICSharpCodeCoreResourceResolver.GetICSharpCodeCoreHostResourceSet(editor.FileName).ResourceFileContent;
                            if (hostContent != null)
                            {
                                content = new MergedResourceFileContent(content, new IResourceFileContent[] { hostContent });
                            }
                        }

                        editor.ShowCompletionWindow(new ResourceCodeCompletionDataProvider(content, this.OutputVisitor, result.CallingClass != null ? result.CallingClass.Name + "." : null), ch);
                        return(true);
                    }
                }
            }

            return(false);
        }
        System.Collections.ICollection IMenuItemBuilder.BuildItems(Codon codon, object owner)
        {
            ITextEditor editor = owner as ITextEditor;

            if (editor == null)
            {
                ITextEditorProvider provider = owner as ITextEditorProvider;
                if (provider == null)
                {
                    return(EmptyControlArray);
                }
                editor = provider.TextEditor;
            }

            ResourceResolveResult result = ResourceResolverService.Resolve(editor, null);

            if (result != null && result.ResourceFileContent != null && result.Key != null)
            {
                List <MenuItem> items = new List <MenuItem>();
                MenuItem        item  = new MenuItem();

                // add resource (if key does not exist) / edit resource (if key exists)
                if (result.ResourceFileContent.ContainsKey(result.Key))
                {
                    item.Header = MenuService.ConvertLabel(StringParser.Parse("${res:Hornung.ResourceToolkit.TextEditorContextMenu.EditResource}"));
                }
                else
                {
                    item.Header = MenuService.ConvertLabel(StringParser.Parse("${res:Hornung.ResourceToolkit.TextEditorContextMenu.AddResource}"));
                }
                item.Click += this.EditResource;
                item.Tag    = result;
                items.Add(item);

                // find references
                item        = new MenuItem();
                item.Header = MenuService.ConvertLabel(StringParser.Parse("${res:SharpDevelop.Refactoring.FindReferencesCommand}"));
                item.Click += this.FindReferences;
                item.Tag    = result;
                items.Add(item);

                // rename
                item        = new MenuItem();
                item.Header = MenuService.ConvertLabel(StringParser.Parse("${res:SharpDevelop.Refactoring.RenameCommand}"));
                item.Click += this.Rename;
                item.Tag    = result;
                items.Add(item);


                // put the resource menu items into a submenu
                // with the resource key as title
                item             = new MenuItem();
                item.Header      = result.Key;
                item.ItemsSource = items;
                return(new System.Windows.Controls.Control[] { item, new Separator() });
            }

            return(EmptyControlArray);
        }
        /// <summary>
        /// Resolves a resource reference.
        /// Line and column are 0-based.
        /// </summary>
        protected ResourceResolveResult Resolve(string fileName, string code, int caretLine, int caretColumn, char?charTyped, bool parseFile)
        {
            this.EnlistTestFile(fileName, code, parseFile);
            IDocument doc = new DocumentFactory().CreateDocument();

            doc.TextContent = code;
            return(ResourceResolverService.Resolve(fileName, doc, caretLine, caretColumn, charTyped));
        }
        /// <summary>
        /// Resolves a resource reference.
        /// Line and column are 0-based.
        /// </summary>
        protected ResourceResolveResult Resolve(string fileName, string code, int caretLine, int caretColumn, char?charTyped, bool parseFile)
        {
            this.EnlistTestFile(fileName, code, parseFile);
            IDocument doc = new AvalonEditDocumentAdapter();

            doc.Text = code;
            return(ResourceResolverService.Resolve(fileName, doc, caretLine, caretColumn, charTyped));
        }
Beispiel #5
0
        public ToolStripItem[] BuildSubmenu(Codon codon, object owner)
        {
            TextEditorControl editor = owner as TextEditorControl;

            if (editor == null)
            {
                return(new ToolStripItem[0]);
            }

            ResourceResolveResult result = ResourceResolverService.Resolve(editor, null);

            if (result != null && result.ResourceFileContent != null && result.Key != null)
            {
                List <ToolStripItem> items = new List <ToolStripItem>();
                MenuCommand          cmd;

                // add resource (if key does not exist) / edit resource (if key exists)
                if (result.ResourceFileContent.ContainsKey(result.Key))
                {
                    cmd = new MenuCommand("${res:Hornung.ResourceToolkit.TextEditorContextMenu.EditResource}", this.EditResource);
                }
                else
                {
                    cmd = new MenuCommand("${res:Hornung.ResourceToolkit.TextEditorContextMenu.AddResource}", this.EditResource);
                }
                cmd.Tag = result;
                items.Add(cmd);

                // find references
                cmd     = new MenuCommand("${res:SharpDevelop.Refactoring.FindReferencesCommand}", this.FindReferences);
                cmd.Tag = result;
                items.Add(cmd);

                // rename
                cmd     = new MenuCommand("${res:SharpDevelop.Refactoring.RenameCommand}", this.Rename);
                cmd.Tag = result;
                items.Add(cmd);


                // put the resource menu items into a submenu
                // with the resource key as title
                ToolStripMenuItem subMenu = new ToolStripMenuItem(result.Key);
                subMenu.DropDownItems.AddRange(items.ToArray());
                return(new ToolStripItem[] { subMenu, new MenuSeparator() });
            }

            return(new ToolStripItem[0]);
        }
Beispiel #6
0
        public ToolTipInfo GetToolTipInfo(TextArea textArea, ToolTipRequestEventArgs e)
        {
            TextLocation logicPos = e.LogicalPosition;
            IDocument    doc      = textArea.Document;

            if (logicPos.X > doc.GetLineSegment(logicPos.Y).Length - 1)
            {
                return(null);
            }

            ResourceResolveResult result = ResourceResolverService.Resolve(textArea.MotherTextEditorControl.FileName, doc, logicPos.Y, logicPos.X, null);

            if (result != null && result.ResourceFileContent != null)
            {
                return(new ToolTipInfo(ResourceResolverService.FormatResourceDescription(result.ResourceFileContent, result.Key)));
            }

            return(null);
        }
        public void HandleToolTipRequest(ToolTipRequestEventArgs e)
        {
            if (!e.InDocument)
            {
                return;
            }

            Location  logicPos = e.LogicalPosition;
            IDocument doc      = e.Editor.Document;

            if (logicPos.X > doc.GetLine(logicPos.Y).Length)
            {
                return;
            }

            ResourceResolveResult result = ResourceResolverService.Resolve(e.Editor.FileName, doc, logicPos.Y - 1, logicPos.X - 1, null);

            if (result != null && result.ResourceFileContent != null)
            {
                e.SetToolTip(ResourceResolverService.FormatResourceDescription(result.ResourceFileContent, result.Key));
            }
        }
        /// <summary>
        /// Finds all references to resources (except the definition) using the specified
        /// <see cref="IResourceReferenceFinder"/> object.
        /// </summary>
        /// <param name="finder">The <see cref="IResourceReferenceFinder"/> to use to find resource references.</param>
        /// <param name="monitor">An object implementing <see cref="IProgressMonitor"/> to report the progress of the operation. Can be <c>null</c>.</param>
        /// <param name="scope">The scope which should be searched.</param>
        /// <returns>A list of references to resources.</returns>
        public static List <Reference> FindReferences(IResourceReferenceFinder finder, IProgressMonitor monitor, SearchScope scope)
        {
            if (finder == null)
            {
                throw new ArgumentNullException("finder");
            }

            if (ParserService.LoadSolutionProjectsThreadRunning)
            {
                if (monitor != null)
                {
                    monitor.ShowingDialog = true;
                }
                MessageService.ShowMessage("${res:SharpDevelop.Refactoring.LoadSolutionProjectsThreadRunning}");
                if (monitor != null)
                {
                    monitor.ShowingDialog = false;
                }
                return(null);
            }

            DateTime startTime = DateTime.UtcNow;

            List <Reference> references = new List <Reference>();

            try {
                NRefactoryAstCacheService.EnableCache();

                ICollection <string> files = GetPossibleFiles(scope);

                if (monitor != null)
                {
                    monitor.BeginTask("${res:SharpDevelop.Refactoring.FindingReferences}", files.Count, true);
                }

                foreach (string fileName in files)
                {
                    if (monitor != null && monitor.IsCancelled)
                    {
                        return(null);
                    }

                    IDocument doc = null;
                    try {
                        // The following line throws an exception if the file does not exist.
                        // But the file may be in an unsaved view content (which would be found by GetDocumentInformation),
                        // so we cannot simply loop on !File.Exists(...).
                        doc = FindReferencesAndRenameHelper.GetDocumentInformation(fileName).CreateDocument();
                    } catch (FileNotFoundException) {
                    }
                    if (doc == null)
                    {
                        if (monitor != null)
                        {
                            ++monitor.WorkDone;
                        }
                        continue;
                    }

                    string fileContent = doc.TextContent;
                    if (String.IsNullOrEmpty(fileContent))
                    {
                        if (monitor != null)
                        {
                            ++monitor.WorkDone;
                        }
                        continue;
                    }

                    int pos = -1;
                    while ((pos = finder.GetNextPossibleOffset(fileName, fileContent, pos)) >= 0)
                    {
                        TextLocation          docPos = doc.OffsetToPosition(pos);
                        ResourceResolveResult rrr    = ResourceResolverService.Resolve(fileName, doc, docPos.Y, docPos.X, null);

                        if (rrr != null && rrr.ResourceFileContent != null)
                        {
                            if (finder.IsReferenceToResource(rrr))
                            {
                                if (rrr.Key != null)
                                {
                                    // The actual location of the key string may be after 'pos' because
                                    // the resolvers may find an expression just before it.
                                    string keyString = rrr.Key;
                                    int    keyPos    = fileContent.IndexOf(keyString, pos, StringComparison.OrdinalIgnoreCase);

                                    if (keyPos < pos)
                                    {
                                        // The key may be escaped in some way in the document.
                                        // Try using the code generator to find this out.
                                        keyPos = FindStringLiteral(fileName, fileContent, rrr.Key, pos, out keyString);
                                    }

                                    if (keyPos < pos)
                                    {
                                        if (monitor != null)
                                        {
                                            monitor.ShowingDialog = true;
                                        }
                                        MessageService.ShowWarning("ResourceToolkit: The key '" + rrr.Key + "' could not be located at the resolved position in the file '" + fileName + "'.");
                                        if (monitor != null)
                                        {
                                            monitor.ShowingDialog = false;
                                        }
                                    }
                                    else
                                    {
                                        references.Add(new Reference(fileName, keyPos, keyString.Length, keyString, rrr));
                                    }
                                }
                                else
                                {
                                    references.Add(new Reference(fileName, pos, 0, null, rrr));
                                }
                            }
                        }
                    }

                    if (monitor != null)
                    {
                        ++monitor.WorkDone;
                    }
                }

                LoggingService.Info("ResourceToolkit: FindReferences finished in " + (DateTime.UtcNow - startTime).TotalSeconds.ToString(System.Globalization.CultureInfo.CurrentCulture) + "s");
            } finally {
                NRefactoryAstCacheService.DisableCache();
                if (monitor != null)
                {
                    monitor.Done();
                }
            }

            return(references);
        }