Avoid showing a link when the underlying path is empty
A common situation when a file is not relevant for a particular result is to return an empty file path location. Currently, we are displaying this situation as a hyperlink in the results, but when clicking on the link, there is an error. To mirror the behaviour of Eclipse, we should avoid showing a link here. This commit changes that behaviour.
This commit is contained in:
@@ -16,17 +16,24 @@ const FILE_LOCATION_REGEX = /file:\/\/(.+):([0-9]+):([0-9]+):([0-9]+):([0-9]+)/;
|
||||
export function tryGetResolvableLocation(
|
||||
loc: LocationValue | undefined
|
||||
): ResolvableLocationValue | undefined {
|
||||
let resolvedLoc;
|
||||
if (loc === undefined) {
|
||||
return undefined;
|
||||
resolvedLoc = undefined;
|
||||
} else if (loc.t === LocationStyle.FivePart && loc.file) {
|
||||
return loc;
|
||||
resolvedLoc = loc;
|
||||
} else if (loc.t === LocationStyle.WholeFile && loc.file) {
|
||||
return loc;
|
||||
resolvedLoc = loc;
|
||||
} else if (loc.t === LocationStyle.String && loc.loc) {
|
||||
return tryGetLocationFromString(loc);
|
||||
resolvedLoc = tryGetLocationFromString(loc);
|
||||
} else {
|
||||
return undefined;
|
||||
resolvedLoc = undefined;
|
||||
}
|
||||
|
||||
if (resolvedLoc && isEmptyPath(resolvedLoc.file)) {
|
||||
resolvedLoc = undefined;
|
||||
}
|
||||
|
||||
return resolvedLoc;
|
||||
}
|
||||
|
||||
export function tryGetLocationFromString(
|
||||
@@ -62,3 +69,14 @@ function isWholeFileMatch(matches: RegExpExecArray): boolean {
|
||||
matches[5] === '0'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the file path is empty. For now, just check whether
|
||||
* the file path is empty. If so, we do not want to render this location
|
||||
* as a link.
|
||||
*
|
||||
* @param path A file path
|
||||
*/
|
||||
function isEmptyPath(path: string) {
|
||||
return !path || path === '/';
|
||||
}
|
||||
|
||||
@@ -119,6 +119,26 @@ describe('interface-utils', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('should resolve a five-part location with an empty path', () => {
|
||||
const mockDatabaseItem: DatabaseItem = ({
|
||||
resolveSourceFile: sinon.stub().returns(vscode.Uri.parse('abc')),
|
||||
} as unknown) as DatabaseItem;
|
||||
|
||||
expect(
|
||||
tryResolveLocation(
|
||||
{
|
||||
t: LocationStyle.FivePart,
|
||||
colStart: 1,
|
||||
colEnd: 3,
|
||||
lineStart: 4,
|
||||
lineEnd: 5,
|
||||
file: '',
|
||||
},
|
||||
mockDatabaseItem
|
||||
)
|
||||
).to.be.undefined;
|
||||
});
|
||||
|
||||
it('should resolve a string location for whole file', () => {
|
||||
const mockDatabaseItem: DatabaseItem = ({
|
||||
resolveSourceFile: sinon.stub().returns(vscode.Uri.parse('abc')),
|
||||
|
||||
Reference in New Issue
Block a user