PHPEntityBase::Ptr_t PHPCodeCompletion::DoGetPHPEntryUnderTheAtPos(IEditor* editor, int pos, bool forFunctionCalltip) { if(!PHPWorkspace::Get()->IsOpen()) return PHPEntityBase::Ptr_t(NULL); pos = editor->GetCtrl()->WordEndPosition(pos, true); // Get the expression under the caret wxString unsavedBuffer = editor->GetTextRange(0, pos); wxString filter; PHPEntityBase::Ptr_t resolved; // Parse the source file PHPSourceFile source(unsavedBuffer); source.SetFilename(editor->GetFileName()); source.SetParseFunctionBody(false); source.Parse(); PHPEntityBase::Ptr_t currentScope = source.CurrentScope(); if(currentScope && currentScope->Is(kEntityTypeClass)) { // we are trying to resolve a 'word' under the caret within the class // body but _not_ within a function body (i.e. it can only be // a definition of some kind) // try to construct an expression that will work int wordStart = editor->GetCtrl()->WordStartPosition(pos, true); wxString theWord = editor->GetTextRange(wordStart, pos); wxString theWordNoDollar = theWord; if(theWord.StartsWith("$")) { theWordNoDollar = theWord.Mid(1); } PHPExpression expr2(unsavedBuffer, "<?php $this->" + theWordNoDollar, forFunctionCalltip); resolved = expr2.Resolve(m_lookupTable, editor->GetFileName().GetFullPath()); filter = expr2.GetFilter(); if(!resolved) { // Maybe its a static member/function/const, try using static keyword PHPExpression expr3(unsavedBuffer, "<?php static::" + theWord, forFunctionCalltip); resolved = expr2.Resolve(m_lookupTable, editor->GetFileName().GetFullPath()); filter = expr2.GetFilter(); } } if(!resolved) { PHPExpression expr(unsavedBuffer, "", forFunctionCalltip); resolved = expr.Resolve(m_lookupTable, editor->GetFileName().GetFullPath()); filter = expr.GetFilter(); } if(resolved && !filter.IsEmpty()) { resolved = m_lookupTable.FindMemberOf(resolved->GetDbId(), filter); if(!resolved) { // Fallback to functions and constants PHPEntityBase::List_t children = m_lookupTable.FindGlobalFunctionAndConsts(PHPLookupTable::kLookupFlags_ExactMatch, filter); if(children.size() == 1) { resolved = *children.begin(); } } if(resolved && resolved->Is(kEntityTypeFunction)) { // for a function, we need to load its children (function arguments) resolved->SetChildren(m_lookupTable.LoadFunctionArguments(resolved->GetDbId())); } else if(resolved && resolved->Is(kEntityTypeFunctionAlias)) { // for a function alias, we need to load the actual functions' children (function arguments) PHPEntityBase::Ptr_t realFunc = resolved->Cast<PHPEntityFunctionAlias>()->GetFunc(); realFunc->SetChildren(m_lookupTable.LoadFunctionArguments(realFunc->GetDbId())); } } return resolved; }
void PHPExpression::Suggest(PHPEntityBase::Ptr_t resolved, PHPLookupTable& lookup, PHPEntityBase::List_t& matches) { // sanity if(!resolved) return; PHPEntityBase::Ptr_t currentScope = GetSourceFile()->CurrentScope(); // GetCount() == 0 && !GetFilter().IsEmpty() i.e. a word completion is required. // We enhance the list with the following: // - PHP keywords // - Global functions // - Global constants // - Function arguments // - Local variables (of the current scope) // - And aliases e.g. 'use foo\bar as Bar;' if(GetCount() == 0 && !GetFilter().IsEmpty()) { // For functions and constants, PHP will fall back to global functions or constants if a // namespaced function or constant does not exist. PHPEntityBase::List_t globals = lookup.FindGlobalFunctionAndConsts(PHPLookupTable::kLookupFlags_Contains, GetFilter()); matches.insert(matches.end(), globals.begin(), globals.end()); if(currentScope && (currentScope->Is(kEntityTypeFunction) || currentScope->Is(kEntityTypeNamespace))) { // If the current scope is a function // add the local variables + function arguments to the current list of matches const PHPEntityBase::List_t& children = currentScope->GetChildren(); PHPEntityBase::List_t::const_iterator iter = children.begin(); for(; iter != children.end(); ++iter) { PHPEntityBase::Ptr_t child = *iter; if(child->Is(kEntityTypeVariable) && child->GetShortName().Contains(GetFilter()) && child->GetShortName() != GetFilter()) { matches.push_back(child); } } } { // Add aliases PHPEntityBase::List_t aliases = GetSourceFile()->GetAliases(); PHPEntityBase::List_t::iterator iter = aliases.begin(); for(; iter != aliases.end(); ++iter) { if((*iter)->GetShortName().Contains(GetFilter())) { matches.push_back(*iter); } } } { // Add $this incase we are inside a class (but only if '$this' contains the filter string) wxString lcFilter = GetFilter().Lower(); if(GetSourceFile()->Class() && wxString("$this").Contains(lcFilter)) { PHPEntityBase::Ptr_t thiz(new PHPEntityVariable()); thiz->SetFullName("$this"); thiz->SetShortName("$this"); thiz->SetFilename(currentScope->GetFilename()); matches.push_back(thiz); } } } // Add the scoped matches // for the code completion size_t flags = PHPLookupTable::kLookupFlags_Contains | GetLookupFlags(); if(resolved->Is(kEntityTypeClass)) { if(resolved->Cast<PHPEntityClass>()->IsInterface() || resolved->Cast<PHPEntityClass>()->IsAbstractClass()) { flags |= PHPLookupTable::kLookupFlags_IncludeAbstractMethods; } } PHPEntityBase::List_t scopeChildren = lookup.FindChildren(resolved->GetDbId(), flags, GetFilter()); matches.insert(matches.end(), scopeChildren.begin(), scopeChildren.end()); // Incase the resolved is a namespace, suggest all children namespaces if(resolved->Is(kEntityTypeNamespace)) { PHPEntityBase::List_t namespaces = lookup.FindNamespaces(resolved->GetFullName(), GetFilter()); matches.insert(matches.end(), namespaces.begin(), namespaces.end()); } // and make the list unique DoMakeUnique(matches); }