add support for static member imports

This commit is contained in:
Dave Holoway
2020-06-16 20:22:19 +01:00
parent bccc29251c
commit 198317a5c2
3 changed files with 55 additions and 10 deletions

View File

@@ -4,7 +4,7 @@
* *
* Each token also contains detailed state information used for completion suggestions. * Each token also contains detailed state information used for completion suggestions.
*/ */
const { JavaType, CEIType, PrimitiveType, ArrayType, UnresolvedType, NullType, TypeVariable } = require('java-mti'); const { JavaType, CEIType, PrimitiveType, ArrayType, UnresolvedType, NullType, TypeVariable, Field, Method } = require('java-mti');
const { SourceType, SourceTypeIdent, SourceField, SourceMethod, SourceConstructor, SourceInitialiser, SourceParameter, SourceAnnotation, const { SourceType, SourceTypeIdent, SourceField, SourceMethod, SourceConstructor, SourceInitialiser, SourceParameter, SourceAnnotation,
SourceUnit, SourcePackage, SourceImport } = require('./source-types2'); SourceUnit, SourcePackage, SourceImport } = require('./source-types2');
const ResolvedImport = require('./parsetypes/resolved-import'); const ResolvedImport = require('./parsetypes/resolved-import');
@@ -2063,6 +2063,19 @@ function findIdentifier(ident, mdecls, scope, imports, typemap) {
}); });
} }
// check static imports
imports.forEach(imp => {
imp.members.forEach(member => {
if (member.name === ident) {
if (member instanceof Field) {
matches.variables.push(member);
} else if (member instanceof Method) {
matches.methods.push(member);
}
}
})
});
const type = mdecls.types.find(t => t.simpleTypeName === ident); const type = mdecls.types.find(t => t.simpleTypeName === ident);
if (type) { if (type) {
matches.types = [type]; matches.types = [type];

View File

@@ -33,7 +33,7 @@ function resolveImportTypes(typenames, import_decl) {
/** /**
* Resolve a single parsed import * Resolve a single parsed import
* *
* @param {Map<string, import('java-mti').JavaType>} typemap * @param {Map<string, import('java-mti').CEIType>} typemap
* @param {string} dotted_name * @param {string} dotted_name
* @param {boolean} is_static * @param {boolean} is_static
* @param {boolean} on_demand * @param {boolean} on_demand
@@ -42,9 +42,31 @@ function resolveImportTypes(typenames, import_decl) {
function resolveSingleImport(typemap, dotted_name, is_static, on_demand, import_kind) { function resolveSingleImport(typemap, dotted_name, is_static, on_demand, import_kind) {
// construct the list of typenames // construct the list of typenames
const typenames = [...typemap.keys()].join('\n'); const typenames = [...typemap.keys()].join('\n');
const matches = fetchImportedTypes(typenames, dotted_name, on_demand);
if (matches) { if (is_static) {
return new ResolvedImport(null, matches, typemap, import_kind); if (on_demand) {
// import all static members - the dotted name must be an exact type
const matches = fetchImportedTypes(typenames, dotted_name, false);
if (matches) {
return new ResolvedImport(null, matches, '*', typemap, import_kind);
}
} else if (dotted_name.includes('.')) {
// the final ident is the static member - the rest is the exact type
const split_name = dotted_name.match(/(.+)\.([^.]+)$/);
const matches = fetchImportedTypes(typenames, split_name[1], false);
if (matches) {
const i = new ResolvedImport(null, matches, split_name[2], typemap, import_kind);
// if there's no matching member, treat it as an invalid import
if (i.members.length > 0) {
return i;
}
}
}
} else {
const matches = fetchImportedTypes(typenames, dotted_name, on_demand);
if (matches) {
return new ResolvedImport(null, matches, null, typemap, import_kind);
}
} }
return null; return null;
} }
@@ -57,7 +79,7 @@ function resolveSingleImport(typemap, dotted_name, is_static, on_demand, import_
* - followed by import declarations (in order of declaration), * - followed by import declarations (in order of declaration),
* - followed by implicit packages * - followed by implicit packages
* *
* @param {Map<string, import('java-mti').JavaType>} androidLibrary * @param {Map<string, import('java-mti').CEIType>} androidLibrary
* @param {import('./source-type').SourceType[]} sourceTypes * @param {import('./source-type').SourceType[]} sourceTypes
* @param {ImportBlock[]} imports list of declared imports in the module * @param {ImportBlock[]} imports list of declared imports in the module
* @param {string} package_name package name of the module * @param {string} package_name package name of the module
@@ -88,14 +110,14 @@ function resolveImports(androidLibrary, sourceTypes, imports, package_name, impl
if (package_name) { if (package_name) {
const matches = fetchImportedTypes(typenames, package_name, true); const matches = fetchImportedTypes(typenames, package_name, true);
if (matches) if (matches)
resolved.push(new ResolvedImport(null, matches, typemap, 'owner-package')); resolved.push(new ResolvedImport(null, matches, null, typemap, 'owner-package'));
} }
// import types from each import declaration // import types from each import declaration
imports.forEach(import_decl => { imports.forEach(import_decl => {
const matches = resolveImportTypes(typenames, import_decl); const matches = resolveImportTypes(typenames, import_decl);
if (matches) { if (matches) {
resolved.push(new ResolvedImport(import_decl, matches, typemap, 'import')); resolved.push(new ResolvedImport(import_decl, matches, null, typemap, 'import'));
} else { } else {
// if we cannot match the import to any types, add it to the unresolved list so // if we cannot match the import to any types, add it to the unresolved list so
// we can flag it as a warning later. // we can flag it as a warning later.
@@ -109,7 +131,7 @@ function resolveImports(androidLibrary, sourceTypes, imports, package_name, impl
implicitPackages.forEach(package_name => { implicitPackages.forEach(package_name => {
const matches = fetchImportedTypes(typenames, package_name, true); const matches = fetchImportedTypes(typenames, package_name, true);
if (matches) if (matches)
resolved.push(new ResolvedImport(null, matches, typemap, 'implicit-import')); resolved.push(new ResolvedImport(null, matches, null, typemap, 'implicit-import'));
}) })
/** /**

View File

@@ -14,10 +14,11 @@ const { ImportBlock } = require('../parser9');
/** /**
* @param {ImportBlock} import_decl * @param {ImportBlock} import_decl
* @param {RegExpMatchArray} matches * @param {RegExpMatchArray} matches
* @param {string} static_ident
* @param {Map<string,CEIType>} typemap * @param {Map<string,CEIType>} typemap
* @param {'owner-package'|'import'|'implicit-import'} import_kind * @param {'owner-package'|'import'|'implicit-import'} import_kind
*/ */
constructor(import_decl, matches, typemap, import_kind) { constructor(import_decl, matches, static_ident, typemap, import_kind) {
/** /**
* The associated import declaration. * The associated import declaration.
* - this value is null for owner-package and implicit-imports * - this value is null for owner-package and implicit-imports
@@ -34,6 +35,15 @@ const { ImportBlock } = require('../parser9');
*/ */
this.types = new Map(matches.map(name => [name, typemap.get(name)])); this.types = new Map(matches.map(name => [name, typemap.get(name)]));
this.members = [];
if (static_ident) {
const type = typemap.get(matches[0]);
if (type) {
type.fields.forEach(f => f.modifiers.includes('static') && (static_ident === '*' || static_ident === f.name) && this.members.push(f));
type.methods.forEach(m => m.modifiers.includes('static') && (static_ident === '*' || static_ident === m.name) && this.members.push(m));
}
}
/** /**
* What kind of import this is: * What kind of import this is:
* - `"owner-package"`: types that are implicitly imported from the same package as the declared module * - `"owner-package"`: types that are implicitly imported from the same package as the declared module