Skip to content

Commit

Permalink
Fixes small hover issues (#860)
Browse files Browse the repository at this point in the history
* Fixes small hover issues

* Lint fixes
  • Loading branch information
markwpearce authored Jul 28, 2023
1 parent 4e78f81 commit eb88ca5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/bscPlugin/hover/HoverProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,5 +376,32 @@ describe('HoverProcessor', () => {
expect(hover?.range).to.eql(util.createRange(6, 26, 6, 34));
expect(hover?.contents).to.eql([fence('someFunc as function')]);
});

it('keeps unresolved types as type names', () => {
const file = program.setFile('source/main.bs', `
sub doSomething(thing as UnknownType)
print thing
end sub
`);
program.validate();
// print thi|ng
let hover = program.getHover(file.srcPath, util.createPosition(2, 30))[0];
expect(hover?.range).to.eql(util.createRange(2, 26, 2, 31));
expect(hover?.contents).eql([fence('thing as UnknownType')]);
});

it('says members on dynamic are dynamic', () => {
const file = program.setFile('source/main.bs', `
sub doSomething(thing)
print thing.member
end sub
`);
program.validate();

// print thing.mem|ber
let hover = program.getHover(file.srcPath, util.createPosition(2, 36))[0];
expect(hover?.range).to.eql(util.createRange(2, 32, 2, 38));
expect(hover?.contents).eql([fence('member as dynamic')]);
});
});
});
8 changes: 6 additions & 2 deletions src/bscPlugin/hover/HoverProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,21 @@ export class HoverProcessor {
const typeFlag = isInTypeExpression ? SymbolTypeFlag.typetime : SymbolTypeFlag.runtime;
const typeChain: TypeChainEntry[] = [];
const exprType = expression.getType({ flags: typeFlag, typeChain: typeChain });

const processedTypeChain = util.processTypeChain(typeChain);
const fullName = processedTypeChain.fullNameOfItem || token.text;
const useCustomTypeHover = isInTypeExpression || expression?.findAncestor(isNewExpression);
let hoverContent = fence(`${fullName} as ${exprType.toString()}`);

// if the type chain has dynamic in it, then just say the token text
const exprNameString = !processedTypeChain.containsDynamic ? fullName : token.text;

let hoverContent = fence(`${exprNameString} as ${exprType.toString()}`);
if (isTypedFunctionType(exprType)) {
exprType.setName(fullName);
hoverContent = this.getFunctionTypeHover(token, expression, exprType, scope);
} else if (useCustomTypeHover && (isClassType(exprType) || isInterfaceType(exprType))) {
hoverContent = this.getCustomTypeHover(exprType, scope);
}

hoverContents.push(hoverContent);

} finally {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,5 +498,6 @@ export interface TypeChainProcessResult {
fullNameOfItem: string;
fullChainName: string;
range: Range;
containsDynamic: boolean;
}

7 changes: 5 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type { CallExpression, CallfuncExpression, DottedGetExpression, FunctionP
import { Logger, LogLevel } from './Logger';
import type { Identifier, Locatable, Token } from './lexer/Token';
import { TokenKind } from './lexer/TokenKind';
import { isBooleanType, isBrsFile, isCallExpression, isCallfuncExpression, isDottedGetExpression, isDoubleType, isDynamicType, isExpression, isFloatType, isIndexedGetExpression, isIntegerType, isInvalidType, isLongIntegerType, isStringType, isTypeExpression, isVariableExpression, isXmlAttributeGetExpression, isXmlFile } from './astUtils/reflection';
import { isBooleanType, isBrsFile, isCallExpression, isCallfuncExpression, isDottedGetExpression, isDoubleType, isDynamicType, isExpression, isFloatType, isIndexedGetExpression, isIntegerType, isInvalidType, isLongIntegerType, isReferenceType, isStringType, isTypeExpression, isVariableExpression, isXmlAttributeGetExpression, isXmlFile } from './astUtils/reflection';
import { WalkMode } from './astUtils/visitors';
import { SourceNode } from 'source-map';
import * as requireRelative from 'require-relative';
Expand Down Expand Up @@ -1673,6 +1673,7 @@ export class Util {
let previousTypeName = '';
let parentTypeName = '';
let errorRange: Range;
let containsDynamic = false;
for (let i = 0; i < typeChain.length; i++) {
const chainItem = typeChain[i];
if (i > 0) {
Expand All @@ -1683,6 +1684,7 @@ export class Util {
fullErrorName = previousTypeName ? `${previousTypeName}.${chainItem.name}` : chainItem.name;
previousTypeName = chainItem.type.toString();
itemName = chainItem.name;
containsDynamic = containsDynamic || (isDynamicType(chainItem.type) && !isReferenceType(chainItem.type));
if (!chainItem.isResolved) {
errorRange = chainItem.range;
break;
Expand All @@ -1693,7 +1695,8 @@ export class Util {
itemParentTypeName: parentTypeName,
fullNameOfItem: fullErrorName,
fullChainName: fullChainName,
range: errorRange
range: errorRange,
containsDynamic: containsDynamic
};
}
}
Expand Down

0 comments on commit eb88ca5

Please sign in to comment.