/// <summary>
        /// Retrieves the refactoring target based on the file.
        /// </summary>
        public static ASResult GetRefactorTargetFromFile(string path, DocumentHelper associatedDocumentHelper)
        {
            String fileName = Path.GetFileNameWithoutExtension(path);
            Int32 line = 0;
            var doc = associatedDocumentHelper.LoadDocument(path);
            ScintillaControl sci = doc != null ? doc.SciControl : null;
            if (sci == null) return null; // Should not happen...
            List<ClassModel> classes = ASContext.Context.CurrentModel.Classes;
            if (classes.Count > 0)
            {
                foreach (ClassModel classModel in classes)
                {
                    if (classModel.Name.Equals(fileName))
                    {
                        // Optimization, we don't need to make a full lookup in this case
                        return new ASResult
                        {
                            IsStatic = true,
                            Type = classModel
                        };
                    }
                }
            }
            else
            {
                foreach (MemberModel member in ASContext.Context.CurrentModel.Members)
                {
                    if (member.Name.Equals(fileName))
                    {
                        line = member.LineFrom;
                        break;
                    }
                }
            }
            if (line > 0)
            {
                sci.SelectText(fileName, sci.PositionFromLine(line));
                return GetDefaultRefactorTarget();
            }

            return null;
        }
        /// <summary>
        /// Checks if a given search match actually points to the given target source
        /// </summary
        /// <returns>True if the SearchMatch does point to the target source.</returns>
        public static bool DoesMatchPointToTarget(ScintillaNet.ScintillaControl Sci, SearchMatch match, ASResult target, DocumentHelper associatedDocumentHelper)
        {
            if (Sci == null || target == null) return false;
            FileModel targetInFile = null;

            if (target.InFile != null)
                targetInFile = target.InFile;
            else if (target.Member != null && target.InClass == null)
                targetInFile = target.Member.InFile;

            Boolean matchMember = targetInFile != null && target.Member != null;
            Boolean matchType = target.Member == null && target.IsStatic && target.Type != null;
            if (!matchMember && !matchType) return false;

            ASResult result = null;
            // get type at match position
            if (match.Index < Sci.Text.Length) // TODO: find out rare cases of incorrect index reported
            {
                result = DeclarationLookupResult(Sci, Sci.MBSafePosition(match.Index) + Sci.MBSafeTextLength(match.Value));
                if (associatedDocumentHelper != null)
                {
                    // because the declaration lookup opens a document, we should register it with the document helper to be closed later
                    associatedDocumentHelper.RegisterLoadedDocument(PluginBase.MainForm.CurrentDocument);
                }
            }
            // check if the result matches the target
            if (result == null || (result.InFile == null && result.Type == null)) return false;
            if (matchMember)
            {
                if (result.Member == null) return false;

                var resultInFile = result.InClass != null ? result.InFile : result.Member.InFile;

                return resultInFile.BasePath == targetInFile.BasePath
                    && resultInFile.FileName == targetInFile.FileName
                    && result.Member.LineFrom == target.Member.LineFrom
                    && result.Member.Name == target.Member.Name;
            }
            else // type
            {
                if (result.Type == null) return false;
                if (result.Type.QualifiedName == target.Type.QualifiedName) return true;
                return false;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Checks if a given search match actually points to the given target source
        /// </summary>
        /// <returns>True if the SearchMatch does point to the target source.</returns>
        public static bool DoesMatchPointToTarget(ScintillaNet.ScintillaControl Sci, SearchMatch match, ASResult target, DocumentHelper associatedDocumentHelper)
        {
            if (Sci == null || target == null)
            {
                return(false);
            }
            FileModel targetInFile = null;

            if (target.InFile != null)
            {
                targetInFile = target.InFile;
            }
            else if (target.Member != null && target.InClass == null)
            {
                targetInFile = target.Member.InFile;
            }

            Boolean matchMember = targetInFile != null && target.Member != null;
            Boolean matchType   = target.Member == null && target.IsStatic && target.Type != null;

            if (!matchMember && !matchType)
            {
                return(false);
            }

            ASResult result = null;

            // get type at match position
            if (match.Index < Sci.Text.Length) // TODO: find out rare cases of incorrect index reported
            {
                result = DeclarationLookupResult(Sci, Sci.MBSafePosition(match.Index) + Sci.MBSafeTextLength(match.Value));
                if (associatedDocumentHelper != null)
                {
                    // because the declaration lookup opens a document, we should register it with the document helper to be closed later
                    associatedDocumentHelper.RegisterLoadedDocument(PluginBase.MainForm.CurrentDocument);
                }
            }
            // check if the result matches the target
            if (result == null || (result.InFile == null && result.Type == null))
            {
                return(false);
            }
            if (matchMember)
            {
                if (result.Member == null)
                {
                    return(false);
                }

                var resultInFile = result.InClass != null ? result.InFile : result.Member.InFile;

                return(resultInFile.BasePath == targetInFile.BasePath &&
                       resultInFile.FileName == targetInFile.FileName &&
                       result.Member.LineFrom == target.Member.LineFrom &&
                       result.Member.Name == target.Member.Name);
            }
            else // type
            {
                if (result.Type == null)
                {
                    return(false);
                }
                if (result.Type.QualifiedName == target.Type.QualifiedName)
                {
                    return(true);
                }
                return(false);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Checks if the given match actually is the declaration.
        /// </summary>
        public static bool IsMatchTheTarget(ScintillaControl Sci, SearchMatch match, ASResult target, DocumentHelper associatedDocumentHelper)
        {
            if (Sci == null || target == null || target.InFile == null || target.Member == null)
            {
                return(false);
            }
            String originalFile = Sci.FileName;
            // get type at match position
            ASResult declaration = DeclarationLookupResult(Sci, Sci.MBSafePosition(match.Index) + Sci.MBSafeTextLength(match.Value), associatedDocumentHelper);

            return((declaration.InFile != null && originalFile == declaration.InFile.FileName) && (Sci.CurrentPos == (Sci.MBSafePosition(match.Index) + Sci.MBSafeTextLength(match.Value))));
        }
Beispiel #5
0
        /// <summary>
        /// Checks if a given search match actually points to the given target source
        /// </summary>
        /// <returns>True if the SearchMatch does point to the target source.</returns>
        public static ASResult DeclarationLookupResult(ScintillaControl Sci, int position, DocumentHelper associatedDocumentHelper)
        {
            if (!ASContext.Context.IsFileValid || (Sci == null))
            {
                return(null);
            }
            // get type at cursor position
            ASResult result = ASComplete.GetExpressionType(Sci, position);

            if (result.IsPackage)
            {
                return(result);
            }
            // open source and show declaration
            if (!result.IsNull())
            {
                if (result.Member != null && (result.Member.Flags & FlagType.AutomaticVar) > 0)
                {
                    return(null);
                }
                FileModel model = result.InFile ?? result.Member?.InFile ?? result.Type?.InFile;
                if (model == null || model.FileName == "")
                {
                    return(null);
                }
                ClassModel inClass = result.InClass ?? result.Type;
                // for Back command
                int lookupLine = Sci.CurrentLine;
                int lookupCol  = Sci.CurrentPos - Sci.PositionFromLine(lookupLine);
                ASContext.Panel.SetLastLookupPosition(ASContext.Context.CurrentFile, lookupLine, lookupCol);
                // open the file
                if (model != ASContext.Context.CurrentModel)
                {
                    if (model.FileName.Length > 0 && File.Exists(model.FileName))
                    {
                        if (!associatedDocumentHelper.ContainsOpenedDocument(model.FileName))
                        {
                            associatedDocumentHelper.LoadDocument(model.FileName);
                        }
                        Sci = associatedDocumentHelper.GetOpenedDocument(model.FileName).SciControl;
                    }
                    else
                    {
                        ASComplete.OpenVirtualFile(model);
                        result.InFile = ASContext.Context.CurrentModel;
                        if (result.InFile == null)
                        {
                            return(null);
                        }
                        if (inClass != null)
                        {
                            inClass = result.InFile.GetClassByName(inClass.Name);
                            if (result.Member != null)
                            {
                                result.Member = inClass.Members.Search(result.Member.Name, 0, 0);
                            }
                        }
                        else if (result.Member != null)
                        {
                            result.Member = result.InFile.Members.Search(result.Member.Name, 0, 0);
                        }
                        Sci = ASContext.CurSciControl;
                    }
                }
                if (Sci == null)
                {
                    return(null);
                }
                if ((inClass == null || inClass.IsVoid()) && result.Member == null)
                {
                    return(null);
                }
                int    line    = 0;
                string name    = null;
                bool   isClass = false;
                // member
                if (result.Member != null && result.Member.LineFrom > 0)
                {
                    line = result.Member.LineFrom;
                    name = result.Member.Name;
                }
                // class declaration
                else if (inClass.LineFrom > 0)
                {
                    line    = inClass.LineFrom;
                    name    = inClass.Name;
                    isClass = true;
                    // constructor
                    foreach (MemberModel member in inClass.Members)
                    {
                        if ((member.Flags & FlagType.Constructor) > 0)
                        {
                            line    = member.LineFrom;
                            name    = member.Name;
                            isClass = false;
                            break;
                        }
                    }
                }
                if (line > 0) // select
                {
                    if (isClass)
                    {
                        ASComplete.LocateMember(Sci, "(class|interface)", name, line);
                    }
                    else
                    {
                        ASComplete.LocateMember(Sci, "(function|var|const|get|set|property|[,(])", name, line);
                    }
                }
                return(result);
            }
            return(null);
        }
 /// <summary>
 /// Checks if a given search match actually points to the given target source
 /// </summary
 /// <returns>True if the SearchMatch does point to the target source.</returns>
 static public bool DoesMatchPointToTarget(ScintillaNet.ScintillaControl Sci, SearchMatch match, ASResult target, DocumentHelper associatedDocumentHelper)
 {
     if (Sci == null || target == null || target.inFile == null || target.Member == null)
     {
         return false;
     }
     // get type at match position
     ASResult result = DeclarationLookupResult(Sci, Sci.MBSafePosition(match.Index) + Sci.MBSafeTextLength(match.Value));
     if (associatedDocumentHelper != null)
     {
         // because the declaration lookup opens a document, we should register it with the document helper to be closed later
         associatedDocumentHelper.RegisterLoadedDocument(PluginBase.MainForm.CurrentDocument);
     }
     // check if the result matches the target
     // TODO: this method of checking their equality seems pretty crude -- is there a better way?
     if (result == null || result.inFile == null || result.Member == null)
     {
         return false;
     }
     Boolean doesMatch = result.inFile.BasePath == target.inFile.BasePath && result.inFile.FileName == target.inFile.FileName && result.Member.LineFrom == target.Member.LineFrom && result.Member.Name == target.Member.Name;
     return (doesMatch);
 }
Beispiel #7
0
        /// <summary>
        /// Checks if a given search match actually points to the given target source
        /// </summary
        /// <returns>True if the SearchMatch does point to the target source.</returns>
        static public bool DoesMatchPointToTarget(ScintillaNet.ScintillaControl Sci, SearchMatch match, ASResult target, DocumentHelper associatedDocumentHelper)
        {
            if (Sci == null || target == null || target.inFile == null || target.Member == null)
            {
                return(false);
            }
            // get type at match position
            ASResult result = DeclarationLookupResult(Sci, Sci.MBSafePosition(match.Index) + Sci.MBSafeTextLength(match.Value));

            if (associatedDocumentHelper != null)
            {
                // because the declaration lookup opens a document, we should register it with the document helper to be closed later
                associatedDocumentHelper.RegisterLoadedDocument(PluginBase.MainForm.CurrentDocument);
            }
            // check if the result matches the target
            // TODO: this method of checking their equality seems pretty crude -- is there a better way?
            if (result == null || result.inFile == null || result.Member == null)
            {
                return(false);
            }
            Boolean doesMatch = result.inFile.BasePath == target.inFile.BasePath && result.inFile.FileName == target.inFile.FileName && result.Member.LineFrom == target.Member.LineFrom && result.Member.Name == target.Member.Name;

            return(doesMatch);
        }