Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provideCodeLenses Error #237237

Closed
andrewmiranda2001 opened this issue Jan 3, 2025 · 10 comments
Closed

provideCodeLenses Error #237237

andrewmiranda2001 opened this issue Jan 3, 2025 · 10 comments
Assignees
Labels
code-lens info-needed Issue requires more information from poster

Comments

@andrewmiranda2001
Copy link

I'm developing an extension and using the Code Lens Provider on a virtual file. However, the provideCodeLenses implementation has an error which is preventing me from actually building the extension to ship out in a new release.

I'm using the most recent version of the VS Code API:

"@types/vscode": "^1.96.0",
...
"vscode": "^1.87.0"

Here is the full error:

Property 'provideCodeLenses' in type 'DiffCodeLensProvider' is not assignable to the same property in base type >'CodeLensProvider'.
Type '(document: TextDocument, token: CancellationToken) => ProviderResult<CodeLens[]>' is not assignable to type '{ (document: >TextDocument, token: CancellationToken): ProviderResult<CodeLens[]>; (document: TextDocument, token: CancellationToken): >ProviderResult<...>; }'.
Type 'ProviderResult<CodeLens[]>' is not assignable to type 'ProviderResult<T[]>'.
Type 'CodeLens[]' is not assignable to type 'ProviderResult<T[]>'.
Type 'CodeLens[]' is not assignable to type 'T[]'.
Type 'CodeLens' is not assignable to type 'T'.
'CodeLens' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'CodeLens'.

I have looked at the API documentation extensively and have looked for other similar open issues to no avail. I've tried uninstalling and reinstalling the VS Code API module. As you can see in the error itself (and by looking at the actual CodeLensProvider interface) the function signature is correct.

import * as vscode from 'vscode';
...
export class DiffCodeLensProvider implements vscode.CodeLensProvider
...
public provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens[]>

When I use the Extension Test Runner, it does complain about the error but when I "Debug Anyway" the Virtual File System and CodeLens on the virtual files apply just fine with no errors logged.

I've done extensive research and still cannot seem to resolve this issue. I've gotten to the point where I am out of development to do and need to push out another release, so I need to be able to build the extension.

Any help would be appreciated.

@RedCMD
Copy link
Contributor

RedCMD commented Jan 3, 2025

just a type mismatch error?

could you link your relevant files please

@andrewmiranda2001
Copy link
Author

andrewmiranda2001 commented Jan 3, 2025

Sure, I can paste in the CodeLens implementation for now. I cannot link directly as the repo is part of a private organization, but I am willing to attach any code you may need:

import * as vscode from 'vscode';
import * as utils  from '../../utils/register';
import { DiffResponse } from '../../adt_api/types/reponse_types';
import { DIFF_TYPES, openVirtualFiles } from '../vfs/server_side_explorer_vfs';
import { diffEquals, gitDiff } from '../../adt_api/commands/git_diff';
import { fetchViews } from '../../adt_api/commands/fetch_info_commands';
import { writeToFileFromUriString } from '../../utils/read_write/write';
import { readFileFromUriString } from '../../utils/read_write/read';
import { getVFSP } from '../../main';



export class DiffCodeLensProvider implements vscode.CodeLensProvider {
    private _onDidChangeCodeLenses = new vscode.EventEmitter<void>();
    readonly onDidChangeCodeLenses: vscode.Event<void> = this._onDidChangeCodeLenses.event;
    private disposables: vscode.Disposable[] = [];

    constructor() {
        // Subscribe to the VFS provider's CodeLens change event
        const SAP_VFSP = getVFSP('construct code lens provider');
        if (!SAP_VFSP) { return; }
        if (SAP_VFSP.onDidChangeCodeLenses) {
            this.disposables.push(
                SAP_VFSP.onDidChangeCodeLenses(() => {
                    this._onDidChangeCodeLenses.fire();
                })
            );
        }
    }

    dispose() {
        this.disposables.forEach(d => d.dispose());
        this._onDidChangeCodeLenses.dispose();
    }

    public provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens[]> {
        const SAP_VFSP = getVFSP('provide code lenses');
        if (!SAP_VFSP) { return; }
        const diffs: DiffResponse[] = SAP_VFSP.getCachedDiff(document.uri.toString()) || [];
        const virtualUriStr = document.uri.toString();
        const localUri = SAP_VFSP.virtualToLocalUriMap.get(virtualUriStr);

        const codeLenses: vscode.CodeLens[] = [];
        if (diffs.length === 0) { return codeLenses; }
        
        for (const diff of diffs) {
            const showLocalContentsCommand: vscode.Command = {
                title: 'Show Local Contents',
                command: 'diff.showLocalContents',
                arguments: [{ diff: diff, localUri: localUri || '' }]
            };

            const revertChangesCommand: vscode.Command = {
                title: 'Revert Changes',
                command: 'diff.revertChange',
                arguments: [{ diffToRevert: diff, localUri: localUri || '', otherDiffs: diffs.filter(d => !diffEquals(d, diff)) }]
            };

            const range = new vscode.Range(diff.diff_start, 0, diff.diff_start, 0);
            codeLenses.push(new vscode.CodeLens(range, showLocalContentsCommand));
            codeLenses.push(new vscode.CodeLens(range, revertChangesCommand));
        }

        const topLineRange = new vscode.Range(0, 0, 0, 0);
        const revertAllChangesCommand: vscode.Command = {
            title: 'Revert All Changes',
            command: 'diff.revertAllChanges',
            arguments: [{localUri: localUri || '', virtualUri: virtualUriStr}]
        };
        const refreshDiffCommand: vscode.Command = {
            title: 'Refresh',
            command: 'diff.refreshDiff',
            arguments: [localUri || '']
        };
        codeLenses.push(new vscode.CodeLens(topLineRange, revertAllChangesCommand));
        codeLenses.push(new vscode.CodeLens(topLineRange, refreshDiffCommand));

        return codeLenses;
    }
}

vscode.commands.registerCommand('diff.showLocalContents', async (args) => {
    const { diff, localUri } = args;
    const line = diff.local_start;
    const uri = vscode.Uri.parse(localUri);
    const doc = await vscode.workspace.openTextDocument(uri);
    const editor = await vscode.window.showTextDocument(doc, { preview: false });
    const position = new vscode.Position(line, 0);
    editor.revealRange(new vscode.Range(position, position), vscode.TextEditorRevealType.InCenter);
    editor.selection = new vscode.Selection(position, position);
    vscode.window.showInformationMessage(`Navigated to local line: ${line + 1}`);
});

vscode.commands.registerCommand('diff.revertChange', async (args) => {
    let { diffToRevert, localUri, otherDiffs } = args;
    diffToRevert = diffToRevert as DiffResponse;

    const newContent = revertDiff(diffToRevert, await readFileFromUriString(localUri));
    await writeToFileFromUriString(localUri, newContent);
    const actualUri = vscode.Uri.parse(localUri);
    const viewMap = new Map<string, { content: string, localUri?: vscode.Uri }>();
    const viewName = utils.getViewName(actualUri);
    viewMap.set(viewName, { content: newContent, localUri: actualUri });

    openVirtualFiles(viewMap, {[viewName]: {success: true, context: otherDiffs}});
});

vscode.commands.registerCommand('diff.revertAllChanges', async (args) => {
    const SAP_VFSP = getVFSP(`revert all changes for ${args.localUri}`);
    if (!SAP_VFSP) { return; }
    const {localUri, virtualUri} = args;
    const viewName = utils.getViewName(vscode.Uri.parse(localUri));
    const response = (await fetchViews([viewName]))[viewName];
    if (!response.success) { vscode.window.showErrorMessage(`Failed to fetch ${viewName}`); return; }
    writeToFileFromUriString(localUri, response.context);
    SAP_VFSP.disposeDocument(vscode.Uri.parse(virtualUri));
});

vscode.commands.registerCommand('diff.refreshDiff', async (localUri: string) => { 
    gitDiff(vscode.Uri.parse(localUri), [vscode.Uri.parse(localUri)]);
});

function revertDiff(diff: DiffResponse, localContent: string): string {
    const localStart = diff.local_start;
    const linesToReplace = diff.type === DIFF_TYPES.MODIFICATION ? diff.changes[1].length : diff.type === DIFF_TYPES.ADDITON ? diff.length : 0;
    const localContents = localContent.split('\n');
    let replacementLines = (diff.type === DIFF_TYPES.MODIFICATION ? diff.changes[0] : diff.type === DIFF_TYPES.ADDITON ? [] : diff.changes) as string[];
    //replacementLines = replacementLines.map(line => line.replace('\r\r', ''));
    localContents.splice(localStart, linesToReplace, ...replacementLines);
    return localContents.join('\n');
}

Again, this should be enough as the issue seems to be directly tied to the vscode module which I have included my version for.

@andrewmiranda2001
Copy link
Author

By the way, I am aware that the vscode module version and the actual vscode engine are different versions. I was just trying to update the API module to see if the latest version saw this issue resolved. This is not the reason it will not package it is still due to the codelens type mismatch.

@RedCMD
Copy link
Contributor

RedCMD commented Jan 3, 2025

I cannot reproduce with just that snippet alone

I see that "vscode": "^1.87.0" doesn't exist
npm error notarget No matching version found for vscode@^1.78.0.

@andrewmiranda2001
Copy link
Author

andrewmiranda2001 commented Jan 3, 2025

VS Code 1.87

It says 1.78 in your error message. Even so, this is not specifying that the extension host has to be 1.87.0, just that it is 1.87.0 or higher. If you need exact versioning, I am running this on vscode version 1.96.2 and @types/[email protected].

I'm not sure how to reproduce the error for you without lending you the entire code repository, which is proprietary.

If you create a virtual environment with the correct specs, does it throw an error?

Here is the file without any of its dependencies so you can set up the environment and just try to see if the import, class declaration, and function declaration appear without error.

import * as vscode from 'vscode';


export class WhateverCodeLensProvider implements vscode.CodeLensProvider {

    public provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens[]> {
        const codeLenses: vscode.CodeLens[] = [];
        return codeLenses;
    }
}

Thank you!

@andrewmiranda2001
Copy link
Author

Image

@andrewmiranda2001
Copy link
Author

PS C:\xxx\xxxxxxxxxx> npm list --depth=0
[email protected] C:\xxx\xxxxxxxxx
├── @types/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├── @typescript-eslint/[email protected]
├── @typescript-eslint/[email protected]
├── @vscode/[email protected]
├── @vscode/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

@RedCMD
Copy link
Contributor

RedCMD commented Jan 3, 2025

after installing all your packages
the error showed up
Image

@RedCMD
Copy link
Contributor

RedCMD commented Jan 3, 2025

caused by "vscode": "1.1.37"

Image

@jrieken jrieken added info-needed Issue requires more information from poster code-lens labels Jan 6, 2025
@andrewmiranda2001
Copy link
Author

That seems to have worked. Thank you so much for all of your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-lens info-needed Issue requires more information from poster
Projects
None yet
Development

No branches or pull requests

3 participants