forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show PyPi link in requirements (#23288)
Show PyPi link in requirements, see #14495. Apart from the test to verify the register of `RequirementsTxtLinkActivator`, there are no other tests. This is my first contribution in TypeScript, if you have an example of a test that could be done, I'm interested :) --------- Co-authored-by: Karthik Nadig <[email protected]>
- Loading branch information
1 parent
8ead7e2
commit 569ca65
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { injectable } from 'inversify'; | ||
import { Hover, languages, TextDocument, Position } from 'vscode'; | ||
import { IExtensionSingleActivationService } from './types'; | ||
|
||
const PYPI_PROJECT_URL = 'https://pypi.org/project'; | ||
|
||
export function generatePyPiLink(name: string): string | null { | ||
// Regex to allow to find every possible pypi package (base regex from https://peps.python.org/pep-0508/#names) | ||
const projectName = name.match(/^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*)($|=| |;|\[)/i); | ||
return projectName ? `${PYPI_PROJECT_URL}/${projectName[1]}/` : null; | ||
} | ||
|
||
@injectable() | ||
export class RequirementsTxtLinkActivator implements IExtensionSingleActivationService { | ||
public readonly supportedWorkspaceTypes = { untrustedWorkspace: true, virtualWorkspace: true }; | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
public async activate(): Promise<void> { | ||
languages.registerHoverProvider([{ pattern: '**/*requirement*.txt' }, { pattern: '**/requirements/*.txt' }], { | ||
provideHover(document: TextDocument, position: Position) { | ||
const link = generatePyPiLink(document.lineAt(position.line).text); | ||
return link ? new Hover(link) : null; | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/test/activation/requirementsTxtLinkActivator.unit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import { expect } from 'chai'; | ||
import { generatePyPiLink } from '../../client/activation/requirementsTxtLinkActivator'; | ||
|
||
suite('Link to PyPi in requiements test', () => { | ||
[ | ||
['pytest', 'pytest'], | ||
['pytest-cov', 'pytest-cov'], | ||
['pytest_cov', 'pytest_cov'], | ||
['pytest_cov[an_extra]', 'pytest_cov'], | ||
['pytest == 0.6.1', 'pytest'], | ||
['pytest== 0.6.1', 'pytest'], | ||
['requests [security] >= 2.8.1, == 2.8.* ; python_version < "2.7"', 'requests'], | ||
['# a comment', null], | ||
['', null], | ||
].forEach(([input, expected]) => { | ||
test(`PyPI link case: "${input}"`, () => { | ||
expect(generatePyPiLink(input!)).equal(expected ? `https://pypi.org/project/${expected}/` : null); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters