forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Show PyPi link in requirements #23288
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
Merged
karthiknadig
merged 10 commits into
microsoft:main
from
LouisGobert:add-pypi-links-in-requirements
May 7, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d18b8c4
Show PyPi link in requirements
LouisGobert 6fbde07
Add repo regex test
LouisGobert 1767fd5
Update src/test/activation/requirementsTxtLinkActivator.unit.test.ts
LouisGobert da2a9ce
Update src/test/activation/requirementsTxtLinkActivator.unit.test.ts
LouisGobert 7e1cddd
Update src/client/activation/requirementsTxtLinkActivator.ts
LouisGobert b57c17c
Update src/client/activation/requirementsTxtLinkActivator.ts
LouisGobert 08ae13c
Fix type issue
LouisGobert e6c0196
Merge branch 'main' into add-pypi-links-in-requirements
LouisGobert eda9374
Fix linting
karthiknadig 900a0a5
Run prettier on requirementsTxtLinkActivator.ts
LouisGobert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.