📄 javadeclmanager.cpp
字号:
// JavaDeclManager.cpp: implementation of the CJavaDeclManager class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "VisualJava.h"
#include "VisualJavaDoc.h"
#include "VisualJavaView.h"
#include "JavaDeclManager.h"
#include "baseTextBuffer.h"
#include "javaapitool.h"
#include "MainFrm.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE(CJavaDeclManager,CDeclManager)
CJavaDeclManager::CJavaDeclManager():CDeclManager()
{
m_bInitParse = false;
m_hImports = NULL;
m_hSource = NULL;
m_hProject = NULL;
m_nImportCount = 0;
m_pCurDeclList = NULL;
CVisualJavaApp::addDeclMgr(this);//make this document available to others, for code-completion
}
////////////////////////////////////////////////////////////////////////////
//Description: //
// this is called for non-threaded version.loads file,parses it //
// and destroys text buffer.Mainly used for code completion. //
////////////////////////////////////////////////////////////////////////////
CJavaDeclManager::CJavaDeclManager(CString strFile):CDeclManager()
{
m_bInitParse = false;//bParseAll;
m_hImports = NULL;
m_hSource = NULL;
m_hProject = NULL;
m_nImportCount = 0;
m_pCurDeclList = NULL;
m_pJavaParser = new CJavaParser(this,false);
m_pParser = m_pJavaParser;
CVisualJavaApp::addDeclMgr(this);
//this is just to parse and get the declarations, destroy afterwards
m_pParserTextBuffer = new CJavaTextBuffer(NULL);
m_pTextBuffer = (CBaseTextBuffer*)m_pParserTextBuffer;
if(m_pParserTextBuffer->LoadFromFile(strFile))
InitParse(strFile);
m_pParserTextBuffer->FreeAll();
delete m_pParserTextBuffer;
delete m_pJavaParser;
m_pJavaParser = NULL;
m_pParserTextBuffer = NULL;
}
CJavaDeclManager::~CJavaDeclManager()
{
for(POSITION p= m_lDecls.GetTailPosition(); p!=NULL;)
delete m_lDecls.GetPrev(p);
if(m_pJavaParser)
delete m_pJavaParser;
if(m_pParserTextBuffer)
{
m_pParserTextBuffer->FreeAll();
delete m_pParserTextBuffer;
RemoveItem(m_hProject);
}
CVisualJavaApp::removeDeclMgr(this);
for(POSITION nPos = m_lImports.GetHeadPosition(); nPos != NULL;)
{
CJavaDeclManager *pDeclMgr = m_lImports.GetNext(nPos);
pDeclMgr->removeExport(this);
}
for(nPos = m_lExports.GetHeadPosition(); nPos != NULL;)
{
CJavaDeclManager *pDeclMgr = m_lExports.GetNext(nPos);
pDeclMgr->removeImport(this);
}
}
HTREEITEM CJavaDeclManager::addItem(CString strItem,HTREEITEM hParent /*= 0*/,
int ntype /*= 0*/,int naccess/*=0*/,DWORD dwData /*= 0*/)
{
if(m_lDecls.GetCount()==0)
{
m_hProject = m_fnPtrAddProject(m_strProject/*+" classes"*/);
m_hSource = m_fnPtrAddSource(m_hProject,"Source: "+m_strSource);
if(ntype == IS_IMPORT)
{
if(m_hImports==NULL)
m_hImports= m_fnPtrAddImport(m_hSource,m_strSource+" Imports...");
}
}
if(ntype == IS_IMPORT)
return m_fnPtrAddItem(strItem,m_hImports,ntype,naccess,dwData);
return m_fnPtrAddItem(strItem,hParent?hParent:m_hSource,ntype,naccess,dwData);
}
void CJavaDeclManager::removeItem(HTREEITEM hItem,int nType /*= -1*/)
{
m_fnPtrRemoveItem(hItem,nType);
//sequence matters
m_hImports = m_fnPtrRemoveOnContainZero(m_hImports);//first
m_hSource = m_fnPtrRemoveOnContainZero(m_hSource);//second
m_hProject = m_fnPtrRemoveOnContainZero(m_hProject);//third
}
void CJavaDeclManager::LoadImport(CImportDecl* pImportDecl,CString strImport)
{
//get working directory for current project,and concanate with import path
CString strWorkingDir = m_strAPIPath+"\\";
CString strImportedFile = strImport;
strImportedFile.Replace('.','\\');//create import path
strImportedFile = strWorkingDir+strImportedFile;
CJavaDeclManager* pImport = CVisualJavaApp::findDeclMgr(strImportedFile+".java");
if(!pImport)
{
if(strImport[strImport.GetLength()-1] == '*')
{
CFileFind finder;
//build a string with wildcards
CString strWildcard(strImportedFile);
strWildcard += _T("\\*.*");
//start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while(bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
CString strFilePath = finder.GetFilePath();
if(finder.IsArchived()||finder.IsNormal())
OpenCompilationUnit(pImportDecl,strFilePath);
}
}else OpenCompilationUnit(pImportDecl,strImportedFile+".java");
}
else
{
//inter_com
pImport->addExport(this);
//add to available import typelist for this document
addImport(pImport);
}
}
void CJavaDeclManager::OpenCompilationUnit(CImportDecl* pImportDecl,CString strFile)
{
CJavaDeclManager* pImportDecl1 = new CJavaDeclManager(strFile);
//add to available typelist for this document
addImport(pImportDecl1);
pImportDecl1->addExport(this);
pImportDecl->setJavaDeclMgr(pImportDecl1);
}
CJavaDeclManager* CJavaDeclManager::LoadCompilationUnit(CString strName)
{
CString strFile = strName;
CString strBasePath = m_strAPIPath+"\\";
if(!BuildTypePath(this,strBasePath,strFile))
{
CPackageDecl* pPackage = NULL;
if(m_lDecls.GetCount()>0)
{
if(m_lDecls.GetHead()->getKind() == PACKAGE_DECL)
pPackage = (CPackageDecl*)m_lDecls.GetHead();
}
if(pPackage)//search within current package
{
CString strPackage = pPackage->getQualifiedName();
strPackage.Replace('.','\\');
strFile = strBasePath+"\\"+strPackage+"\\"+strName+".java";
CFileFind finder;
if(!finder.FindFile(strFile))
return NULL;
}else return NULL;
}
//through all we've done so far,we now know this type can be acessed
//within this document,so let search global list first to see,if it
//has already been loaded.
CJavaDeclManager* pDeclMgr = theApp.findDeclMgr(strFile);
if(pDeclMgr)
{
addImport(pDeclMgr);pDeclMgr->addExport(this);
return pDeclMgr;
}
pDeclMgr = new CJavaDeclManager(strFile);
//add to available typelist for this document
//*************************************************//
/**/addImport(pDeclMgr); /**/
/**/pDeclMgr->addExport(this); /**/
//*************************************************//
return pDeclMgr;
}
void CJavaDeclManager::InitParse(CString strPathName)
{
m_strPathName = strPathName;
m_strFilePath = strPathName;
CString strFile(strPathName);
for(int I=strFile.GetLength()-1;I>=0;I--)
{
if(strFile[I] == '.')
{
--I;
{
for(I; ((strFile[I] !='\\')&&(I>=0)); I--)
m_strSource.Insert(0,strFile[I]);
//m_strSource += ".java";
}
break;
}
}
//***
CString strClockFormat(_T("%H:%M:%S"));
TRACE0("START_TIME: "+CTime::GetCurrentTime ().Format (strClockFormat)+" :"+m_strSource+"\n");
m_pJavaParser->parseOnFileLoaded();
TRACE0("END_TIME: "+CTime::GetCurrentTime ().Format (strClockFormat)+"\n");
m_bInitParse = false;
}
void CJavaDeclManager::LoadCCFrame(CTypeDecl* pType,int x,int y,
CVisualJavaView* pView)
{
CPoint pt(x,y);
CPoint ptText = pView->TextToClientExt(pt);
CRect rcCaller(ptText.x,ptText.y,ptText.x-(2*pView->GetCharWidthExt()),ptText.y+(pView->GetLineHeightExt()));
CPopup* pPopup = (CPopup*)RUNTIME_CLASS(CPopup)->CreateObject();
CMainFrame* pFrm = (CMainFrame*)AfxGetMainWnd();
DECL_LIST *pList = pType->getMembers();
pFrm->m_wndCodeCompletionList.Reset();
for(POSITION pos = pList->GetHeadPosition(); pos !=NULL;)
{
__CBaseDecl* pMem = pList->GetNext(pos);
switch(pMem->getKind())
{
case FIELD_DECL:
{
CJavaFieldDecl* pField = (CJavaFieldDecl*)pMem;
int nVarCount = pField->getVarCount();
for(int I=0; I<nVarCount; I++)
pFrm->m_wndCodeCompletionList.AddItem(pField->getVar(I),IS_VAR,pField->getAccess());
}break;
case METHOD_DECL:
pFrm->m_wndCodeCompletionList.AddItem(pMem->getDeclName(),IS_FUNC,pMem->getAccess());
break;
case PACKAGE_DECL:
case CLASS_DECL:
pFrm->m_wndCodeCompletionList.AddItem(pMem->getDeclName(),IS_CLASS);
break;
case INTERFACE_DECL:;
}
}
pView->ClientToScreen(rcCaller);
CRect rcRect;
pFrm->m_wndCodeCompletionList.ReCalcSize(rcRect);
//≒opup the TreeCtrl
VERIFY(pPopup->Display (&(pFrm->m_wndCodeCompletionList), pView, rcCaller, &(pFrm->m_wndCodeCompletionList.m_xPopupCtrlEvent)));
}
void CJavaDeclManager::OnInfoRequestChar(int nCharIndex,int nLine,char nChar,
CVisualJavaView* pView)
{
CString strType;
if(nChar == '.')
{
if(m_pJavaParser->OnDotImpl(nCharIndex,nLine,strType))
{
CStringList lstrTypes;
CVisualJavaApp::Tokenize(&lstrTypes,strType,'.');
CTypeDecl* pType = FindType(lstrTypes,m_pCCBlock);
if(pType != NULL)
LoadCCFrame(pType,nCharIndex,nLine,pView);
}
}
else
if(nChar == '(')
{
CString strMethodName;
if(m_pJavaParser->OnCallMethod(nCharIndex,nLine,strType,strMethodName))
{
CPoint pt(nCharIndex,nLine);
CPoint ptText = pView->TextToClientExt(pt);
CRect rcCaller(ptText.x,ptText.y,ptText.x-(2*pView->GetCharWidthExt()),ptText.y+(pView->GetLineHeightExt()));
CMainFrame* pFrm = (CMainFrame*)AfxGetMainWnd();
CPopup* pPopup = (CPopup*)RUNTIME_CLASS(CPopup)->CreateObject();
pView->ClientToScreen(rcCaller);
VERIFY(pPopup->Display (&(pFrm->m_wndParamView), pView, rcCaller, &(pFrm->m_wndParamView.m_xPopupCtrlEvent)));
}
}
}
CTypeDecl* CJavaDeclManager::FindType(CStringList& lstrTypes,
CBlockDecl* pParent/*=NULL*/)
{
if(pParent)
{
CTypeDecl* pType = pParent->FindType(lstrTypes,lstrTypes.GetHeadPosition());
if(pType)return pType;
else return FindType(lstrTypes);
}
else
{
POSITION nStrPos = lstrTypes.GetHeadPosition();
CString strName = lstrTypes.GetNext(nStrPos);
for(POSITION pos = m_lTypeDecls.GetHeadPosition(); pos != NULL;)
{
CTypeDecl* pType = m_lTypeDecls.GetNext(pos);
if(pType->getDeclName().Compare(strName) == 0)
{
if(nStrPos)
{
CTypeDecl* pFoundType = pType->FindType(lstrTypes,nStrPos);
if(pFoundType)return pFoundType;
}else return pType;
}
}
}
//search imports
POSITION nPos = m_lImports.GetHeadPosition();
for(nPos; nPos != NULL;)
{
CJavaDeclManager* pMgr = m_lImports.GetNext(nPos);
CTypeDecl* pType = pMgr->FindType(lstrTypes);
if(pType != NULL)
return pType;
}return NULL;
/**
if(pParent != NULL)
return FindInClass(pParent,lstrTypes,lstrTypes.GetHeadPosition());
this->m_lpCurTypeList = &this->m_lTypeDecls;
return FindTypeFromTopLevel(lstrTypes);
**/
}
CTypeDecl* CJavaDeclManager::FindType(TYPE_LIST* pTypeList,
CStringList& lstrTypes,
CBlockDecl* pParent /*=NULL*/)
{
if(pParent)
{
CTypeDecl* pType = pParent->FindType(lstrTypes,lstrTypes.GetHeadPosition());
if(pType)return pType;
else return FindType(lstrTypes);
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -