/** * Get Item from {@link IField} <br> * DLTK Model => AST */ public static Item getItem(IField field) { IModelElement parent = field.getParent(); if (parent instanceof IType) { RecordTypeDef typeDef = (RecordTypeDef) getTypeDef((IType) parent); return typeDef.getFields().get(field.getElementName()); } else if (parent instanceof ISourceModule) { LuaSourceRoot luaSourceRoot = getLuaSourceRoot((ISourceModule) parent); try { if (Flags.isPrivate(field.getFlags())) { List<LocalVar> localVars = luaSourceRoot.getInternalContent().getContent().getLocalVars(); for (LocalVar localVar : localVars) { if (localVar.getVar().getName().equals(field.getElementName())) return localVar.getVar(); } } else { return luaSourceRoot.getFileapi().getGlobalvars().get(field.getElementName()); } } catch (ModelException e) { Activator.logError("unable to get item from field " + field, e); // $NON-NLS-1$ return null; } } return null; }
/** * Get IMember from Item <br> * AST => DLTK Model */ public static IMember getIMember(ISourceModule sourceModule, Item item) { LuaASTNode parent = item.getParent(); if (LuaASTUtils.isTypeField(item)) { // support record field IType iType = getIType(sourceModule, (RecordTypeDef) parent); if (iType != null) { try { for (IModelElement child : iType.getChildren()) { if (child.getElementName().equals(item.getName()) && child instanceof IMember) return (IMember) child; } } catch (ModelException e) { Activator.logWarning( "unable to get IMember corresponding to the given item " + item, e); // $NON-NLS-1$ } } } else if (LuaASTUtils.isLocal(item)) { // TODO retrieve local var which are in the model (so the local var in the first block) // support local variable // ------------------------------------------------------------------------------------ // manage method TypeDef resolvedtype = LuaASTUtils.resolveTypeLocaly(sourceModule, item); if (resolvedtype != null && resolvedtype instanceof FunctionTypeDef) { FunctionTypeDef functionResolvedType = (FunctionTypeDef) resolvedtype; String[] parametersName = new String[functionResolvedType.getParameters().size()]; for (int i = 0; i < parametersName.length; i++) { parametersName[i] = functionResolvedType.getParameters().get(i).getName(); } return new FakeMethod( sourceModule, item.getName(), item.sourceStart(), item.getName().length(), parametersName, Declaration.AccPrivate, item); } // manage field return new FakeField( sourceModule, item.getName(), item.sourceStart(), item.getName().length(), Declaration.AccPrivate, item); } else if (LuaASTUtils.isGlobal(item)) { // support global var try { for (IModelElement child : sourceModule.getChildren()) { if (child.getElementName().equals(item.getName()) && child instanceof IMember) return (IMember) child; } } catch (ModelException e) { Activator.logWarning( "unable to get IMember corresponding to the given item " + item, e); // $NON-NLS-1$ } } else if (LuaASTUtils.isUnresolvedGlobal(item)) { return new FakeField( sourceModule, item.getName(), item.sourceStart(), item.getName().length(), Declaration.AccGlobal, item); } return null; }