GSOC work, code simplification

Created a new class: UnoTypeCodeCompletor, which does the field/method extraction for code completition.
Functions GetXIdlClassMethods() and GetXIdlClassFields() have been added to UnoTypeCodeCompletor and removed from EditorWindow.

Change-Id: Ia47965b502c868f52c466c282cb98ccd806ff6f7
This commit is contained in:
Gergo Mocsi 2013-08-07 11:49:33 +02:00
parent 6a2aa97cc1
commit 3345726d31
2 changed files with 173 additions and 116 deletions

View File

@ -54,6 +54,10 @@ class SvxSearchItem;
#include <vcl/textdata.hxx> #include <vcl/textdata.hxx>
#include <basic/codecompletecache.hxx> #include <basic/codecompletecache.hxx>
#include "com/sun/star/reflection/XIdlClass.hpp" #include "com/sun/star/reflection/XIdlClass.hpp"
#include <comphelper/namedvaluecollection.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/configurationhelper.hxx>
#include "com/sun/star/reflection/XIdlReflection.hpp"
namespace com { namespace sun { namespace star { namespace beans { namespace com { namespace sun { namespace star { namespace beans {
class XMultiPropertySet; class XMultiPropertySet;
@ -120,8 +124,6 @@ private:
CodeCompleteDataCache aCodeCompleteCache; CodeCompleteDataCache aCodeCompleteCache;
boost::scoped_ptr< CodeCompleteWindow > pCodeCompleteWnd; boost::scoped_ptr< CodeCompleteWindow > pCodeCompleteWnd;
OUString GetActualSubName( sal_uLong nLine ); // gets the actual subroutine name according to line number OUString GetActualSubName( sal_uLong nLine ); // gets the actual subroutine name according to line number
std::vector< OUString > GetXIdlClassMethods( ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XIdlClass > xClass ) const;
std::vector< OUString > GetXIdlClassFields( ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XIdlClass > xClass ) const;
void HandleAutoCorrect(); void HandleAutoCorrect();
void HandleAutoCloseParen(); void HandleAutoCloseParen();
void HandleAutoCloseDoubleQuotes(); void HandleAutoCloseDoubleQuotes();
@ -537,6 +539,27 @@ public:
}; };
class UnoTypeCodeCompletetor
{
private:
::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xFactory;
::com::sun::star::uno::Reference< ::com::sun::star::reflection::XIdlReflection > xRefl;
::com::sun::star::uno::Reference< ::com::sun::star::reflection::XIdlClass > xClass;
bool bCanComplete;
bool CheckField( const OUString& sFieldName );
bool CheckMethod( const OUString& sMethName );
public:
UnoTypeCodeCompletetor( const std::vector< OUString >& aVect, const OUString& sVarType );
~UnoTypeCodeCompletetor(){}
std::vector< OUString > GetXIdlClassMethods() const;
std::vector< OUString > GetXIdlClassFields() const;
bool CanCodeComplete() const;
};
} // namespace basctl } // namespace basctl
#endif // BASCTL_BASIDE2_HXX #endif // BASCTL_BASIDE2_HXX

View File

@ -48,13 +48,10 @@
#include <vcl/help.hxx> #include <vcl/help.hxx>
#include <vector> #include <vector>
#include "com/sun/star/reflection/XIdlReflection.hpp"
#include <comphelper/namedvaluecollection.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/configurationhelper.hxx>
#include "com/sun/star/reflection/XInterfaceMemberTypeDescription.hpp" #include "com/sun/star/reflection/XInterfaceMemberTypeDescription.hpp"
#include "com/sun/star/reflection/XIdlMethod.hpp" #include "com/sun/star/reflection/XIdlMethod.hpp"
#include "com/sun/star/reflection/XIdlField.hpp" #include "com/sun/star/reflection/XIdlField.hpp"
#include "com/sun/star/uno/Exception.hpp"
namespace basctl namespace basctl
{ {
@ -744,7 +741,7 @@ void EditorWindow::HandleCodeCompletition()
HighlightPortions aPortions; HighlightPortions aPortions;
aHighlighter.getHighlightPortions( nLine, aLine, aPortions ); aHighlighter.getHighlightPortions( nLine, aLine, aPortions );
if( aPortions.size() != 0 ) if( aPortions.size() != 0 )
{ {//use the syntax highlighter to grab out nested reflection calls, eg. aVar.aMethod("aa").aOtherMethod ..
for ( size_t i = 0; i < aPortions.size(); i++ ) for ( size_t i = 0; i < aPortions.size(); i++ )
{ {
HighlightPortion& r = aPortions[i]; HighlightPortion& r = aPortions[i];
@ -765,124 +762,45 @@ void EditorWindow::HandleCodeCompletition()
pEditView->SetSelection( aSel ); pEditView->SetSelection( aSel );
} }
Reference< lang::XMultiServiceFactory > xFactory( comphelper::getProcessServiceFactory(), UNO_SET_THROW ); UnoTypeCodeCompletetor aTypeCompletor( aVect, sVarType );
Reference< reflection::XIdlReflection > xRefl( xFactory->createInstance("com.sun.star.reflection.CoreReflection"), UNO_QUERY_THROW );
if( xRefl.is() ) if( aTypeCompletor.CanCodeComplete() )
{ {
Reference< reflection::XIdlClass > xClass = xRefl->forName(sVarType);//get the base class for reflection std::vector< OUString > aEntryVect;//entries to be inserted into the list
if( xClass != NULL ) std::vector< OUString > aFieldVect = aTypeCompletor.GetXIdlClassFields();//fields
aEntryVect.insert(aEntryVect.end(), aFieldVect.begin(), aFieldVect.end() );
if( CodeCompleteOptions::IsExtendedTypeDeclaration() )
{// if extended types on, reflect classes, else just the structs (XIdlClass without methods)
std::vector< OUString > aMethVect = aTypeCompletor.GetXIdlClassMethods();//methods
aEntryVect.insert(aEntryVect.end(), aMethVect.begin(), aMethVect.end() );
}
if( aEntryVect.size() > 0 )
{ {
unsigned int j = 1; // calculate position
OUString sMethName; Rectangle aRect = ( (TextEngine*) GetEditEngine() )->PaMtoEditCursor( aSel.GetEnd() , false );
bool bReflect = true; long nViewYOffset = pEditView->GetStartDocPos().Y();
while( j != aVect.size() ) Point aPoint = aRect.BottomRight();
aPoint.Y() = (aPoint.Y() - nViewYOffset) + 2;
aSel.GetStart().GetIndex() += 1;
aSel.GetEnd().GetIndex() += 1;
pCodeCompleteWnd->ClearListBox();
pCodeCompleteWnd->SetTextSelection(aSel);
//fill the listbox
for(unsigned int l = 0; l < aEntryVect.size(); ++l)
{ {
sMethName = aVect[j]; pCodeCompleteWnd->InsertEntry( aEntryVect[l] );
Reference< reflection::XIdlField> xField = xClass->getField( sMethName );
if( xField != NULL )
{
xClass = xField->getType();
if( xClass == NULL )
{
break;
}
}
else
{
if( CodeCompleteOptions::IsExtendedTypeDeclaration() )
{
Reference< reflection::XIdlMethod> xMethod = xClass->getMethod( sMethName );
if( xMethod != NULL ) //method OK
{
xClass = xMethod->getReturnType();
if( xClass == NULL )
{
break;
}
}
else
{//nothing to reflect
bReflect = false;
break;
}
}
else
{// no extended types allowed
bReflect = false;
break;
}
}
j++;
}
if( bReflect )
{
std::vector< OUString > aEntryVect;//entries to be inserted into the list
std::vector< OUString > aMethVect = GetXIdlClassMethods(xClass);//methods
std::vector< OUString > aFieldVect = GetXIdlClassFields(xClass);//fields
aEntryVect.insert(aEntryVect.end(), aFieldVect.begin(), aFieldVect.end() );
if( CodeCompleteOptions::IsExtendedTypeDeclaration() )
{// if extended types on, reflect classes, else just the structs (XIdlClass without methods)
aEntryVect.insert(aEntryVect.end(), aMethVect.begin(), aMethVect.end() );
}
if( aEntryVect.size() > 0 )
{
// calculate position
Rectangle aRect = ( (TextEngine*) GetEditEngine() )->PaMtoEditCursor( aSel.GetEnd() , false );
long nViewYOffset = pEditView->GetStartDocPos().Y();
Point aPoint = aRect.BottomRight();
aPoint.Y() = aPoint.Y() - nViewYOffset;
aPoint.Y() += 2;
aSel.GetStart().GetIndex() += 1;
aSel.GetEnd().GetIndex() += 1;
pCodeCompleteWnd->ClearListBox();
pCodeCompleteWnd->SetTextSelection(aSel);
//fill the listbox
for(unsigned int l = 0; l < aEntryVect.size(); ++l)
{
pCodeCompleteWnd->InsertEntry( aEntryVect[l] );
}
//show it
pCodeCompleteWnd->SetPosPixel( aPoint );
pCodeCompleteWnd->Show();
pCodeCompleteWnd->ResizeListBox();
pCodeCompleteWnd->SelectFirstEntry();
pEditView->GetWindow()->GrabFocus();
}
} }
//show it
pCodeCompleteWnd->SetPosPixel( aPoint );
pCodeCompleteWnd->Show();
pCodeCompleteWnd->ResizeListBox();
pCodeCompleteWnd->SelectFirstEntry();
pEditView->GetWindow()->GrabFocus();
} }
} }
} }
} }
std::vector< OUString > EditorWindow::GetXIdlClassMethods( Reference< reflection::XIdlClass > xClass ) const
{
Sequence< Reference< reflection::XIdlMethod > > aMethods = xClass->getMethods();
std::vector< OUString > aRetVect;
if( aMethods.getLength() != 0 )
{
for(sal_Int32 l = 0; l < aMethods.getLength(); ++l)
{
aRetVect.push_back(OUString(aMethods[l]->getName()));
}
}
return aRetVect;
}
std::vector< OUString > EditorWindow::GetXIdlClassFields( Reference< reflection::XIdlClass > xClass ) const
{
Sequence< Reference< reflection::XIdlField > > aFields = xClass->getFields();
std::vector< OUString > aRetVect;
if( aFields.getLength() != 0 )
{
for(sal_Int32 l = 0; l < aFields.getLength(); ++l)
{
aRetVect.push_back(OUString(aFields[l]->getName()));
}
}
return aRetVect;
}
void EditorWindow::Paint( const Rectangle& rRect ) void EditorWindow::Paint( const Rectangle& rRect )
{ {
if ( !pEditEngine ) // We need it now at latest if ( !pEditEngine ) // We need it now at latest
@ -2777,7 +2695,7 @@ void CodeCompleteWindow::ResizeListBox()
aLongestEntry = pListBox->GetEntry( i ); aLongestEntry = pListBox->GetEntry( i );
} }
} }
long nWidth = GetTextWidth(aLongestEntry); //long nWidth = GetTextWidth(aLongestEntry);
sal_uInt16 nColumns = aLongestEntry.getLength(); sal_uInt16 nColumns = aLongestEntry.getLength();
sal_uInt16 nLines = std::min( (sal_uInt16) 6, pListBox->GetEntryCount() ); sal_uInt16 nLines = std::min( (sal_uInt16) 6, pListBox->GetEntryCount() );
const Font& aFont = pListBox->GetUnzoomedControlPointFont(); const Font& aFont = pListBox->GetUnzoomedControlPointFont();
@ -2836,6 +2754,122 @@ void CodeCompleteWindow::SetVisibleEntries()
pListBox->SetVisibleEntries(); pListBox->SetVisibleEntries();
} }
UnoTypeCodeCompletetor::UnoTypeCodeCompletetor( const std::vector< OUString >& aVect, const OUString& sVarType )
: bCanComplete( true )
{
if( aVect.size() == 0 || sVarType.isEmpty() )
{
bCanComplete = false;//invalid parameters, nothing to code complete
return;
}
try
{
xFactory = Reference< lang::XMultiServiceFactory >( comphelper::getProcessServiceFactory(), UNO_QUERY_THROW );
xRefl = Reference< reflection::XIdlReflection >( xFactory->createInstance("com.sun.star.reflection.CoreReflection"), UNO_QUERY_THROW );
if( xRefl.is() )
xClass = xRefl->forName( sVarType );//get the base class for reflection
}
catch( const Exception& ex)
{
bCanComplete = false;
return;
}
unsigned int j = 1;//start from aVect[1]: aVect[0] is the variable name
OUString sMethName;
while( j != aVect.size() )
{
sMethName = aVect[j];
if( !CheckField(sMethName) )//check field
break;
else
{
if( CodeCompleteOptions::IsExtendedTypeDeclaration() )
{// if extended types on, check methods
if( !CheckMethod(sMethName) )
{
bCanComplete = false;
break;
}
}
bCanComplete = false;
break;
}
j++;
}
}
std::vector< OUString > UnoTypeCodeCompletetor::GetXIdlClassMethods() const
{
std::vector< OUString > aRetVect;
if( bCanComplete )
{
Sequence< Reference< reflection::XIdlMethod > > aMethods = xClass->getMethods();
if( aMethods.getLength() != 0 )
{
for(sal_Int32 l = 0; l < aMethods.getLength(); ++l)
{
aRetVect.push_back(OUString(aMethods[l]->getName()));
}
}
}
return aRetVect;//this is empty when cannot code complete
}
std::vector< OUString > UnoTypeCodeCompletetor::GetXIdlClassFields() const
{
std::vector< OUString > aRetVect;
if( bCanComplete )
{
Sequence< Reference< reflection::XIdlField > > aFields = xClass->getFields();
if( aFields.getLength() != 0 )
{
for(sal_Int32 l = 0; l < aFields.getLength(); ++l)
{
aRetVect.push_back(OUString(aFields[l]->getName()));
}
}
}
return aRetVect;//this is empty when cannot code complete
}
bool UnoTypeCodeCompletetor::CanCodeComplete() const
{
return bCanComplete;
}
bool UnoTypeCodeCompletetor::CheckField( const OUString& sFieldName )
{
Reference< reflection::XIdlField> xField = xClass->getField( sFieldName );
if( xField != NULL )
{
xClass = xField->getType();
if( xClass == NULL )
{
return true;
}
}
return false;
}
bool UnoTypeCodeCompletetor::CheckMethod( const OUString& sMethName )
{
Reference< reflection::XIdlMethod> xMethod = xClass->getMethod( sMethName );
if( xMethod != NULL ) //method OK
{
xClass = xMethod->getReturnType();
if( xClass == NULL )
{
return true;
}
}
return false;
}
} // namespace basctl } // namespace basctl
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */