Skip to content

Commit

Permalink
add Directory.Packages.props
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandoescolar committed Nov 29, 2024
1 parent 0f06ace commit 157489e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 34 deletions.
47 changes: 27 additions & 20 deletions src/language/LanguageExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NugetVersionCodeActionProvider } from "./actions/NugetVersionCodeAction
import { NugetNameCompletionItemProvider } from "./completions/NugetNameCompletionItemProvider";
import { NugetVersionCompletionItemProvider } from "./completions/NugetVersionCompletionItemProvider";
import { CodeDecoratorController } from "./decorators/CodeDecoratorController";
import { CSPROJ } from "./filters";
import { CSPROJ, PACKAGES_PROPS } from "./filters";
import { NugetVersionDecorator } from "./decorators/NugetVersionDecorator";

export class LanguageExtensions {
Expand All @@ -12,27 +12,34 @@ export class LanguageExtensions {
}

register() {
const decorator = new CodeDecoratorController(this.context, [
new NugetVersionDecorator()
]);
const fileTypes = [
{ filter: CSPROJ, tagName: "PackageReference"},
{ filter: PACKAGES_PROPS, tagName: "PackageVersion"}
];

const decorator = new CodeDecoratorController(this.context,
fileTypes.map(({filter, tagName}) => new NugetVersionDecorator(filter, tagName))
);
decorator.register();

this.context.subscriptions.push(vscode.languages.registerCodeActionsProvider(
CSPROJ,
new NugetVersionCodeActionProvider(),
));

this.context.subscriptions.push(vscode.languages.registerCompletionItemProvider(
CSPROJ,
new NugetNameCompletionItemProvider(),
'.', '"'
));

this.context.subscriptions.push(vscode.languages.registerCompletionItemProvider(
CSPROJ,
new NugetVersionCompletionItemProvider(),
'.', '"'
));
fileTypes.forEach(({filter, tagName}) => {
this.context.subscriptions.push(vscode.languages.registerCodeActionsProvider(
filter,
new NugetVersionCodeActionProvider(tagName),
));

this.context.subscriptions.push(vscode.languages.registerCompletionItemProvider(
filter,
new NugetNameCompletionItemProvider(tagName),
'.', '"'
));

this.context.subscriptions.push(vscode.languages.registerCompletionItemProvider(
filter,
new NugetVersionCompletionItemProvider(tagName),
'.', '"'
));
});
}

}
9 changes: 7 additions & 2 deletions src/language/actions/NugetVersionCodeActionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import * as vscode from "vscode";
import * as nuget from '@extensions/nuget';

export class NugetVersionCodeActionProvider implements vscode.CodeActionProvider {
private readonly regEx: RegExp;

constructor(tagName: string) {
this.regEx = new RegExp(`<${tagName} Include="(.*)" Version="(.*)"`);
}

async provideCodeActions(document: vscode.TextDocument, range: vscode.Range | vscode.Selection, context: vscode.CodeActionContext, token: vscode.CancellationToken): Promise<(vscode.CodeAction | vscode.Command)[] | null | undefined> {
const line = document.lineAt(range.start.line).text;
const regEx = /<PackageReference Include="(.*)" Version="(.*)" /;
const match = regEx.exec(line);
const match = this.regEx.exec(line);
if (!match) {
return undefined;
}
Expand Down
9 changes: 7 additions & 2 deletions src/language/completions/NugetNameCompletionItemProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import * as vscode from "vscode";
import * as nuget from '@extensions/nuget';

export class NugetNameCompletionItemProvider implements vscode.CompletionItemProvider {
private readonly regEx: RegExp;

constructor(tagName: string) {
this.regEx = new RegExp(`<${tagName} Include="([^"\n]*)$`);
}

async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext): Promise<vscode.CompletionItem[] | vscode.CompletionList<vscode.CompletionItem> | null | undefined> {
const linePrefix = document.lineAt(position).text.substr(0, position.character);
const regEx = /<PackageReference Include="([^"\n]*)$/;
const match = regEx.exec(linePrefix);
const match = this.regEx.exec(linePrefix);
if (!match) {
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import * as vscode from "vscode";
import * as nuget from '@extensions/nuget';

export class NugetVersionCompletionItemProvider implements vscode.CompletionItemProvider {
private readonly regEx: RegExp;

constructor(tagName: string) {
this.regEx = new RegExp(`<${tagName} Include="(.*)" `);
}

async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext): Promise<vscode.CompletionItem[] | vscode.CompletionList<vscode.CompletionItem> | null | undefined> {
const linePrefix = document.lineAt(position).text.substr(0, position.character);
const regEx = /<PackageReference Include="(.*)" /;
const match = regEx.exec(linePrefix);
const match = this.regEx.exec(linePrefix);
if (!match) {
return undefined;
}
Expand Down
14 changes: 7 additions & 7 deletions src/language/decorators/NugetVersionDecorator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as vscode from "vscode";
import * as nuget from "@extensions/nuget";
import { ICodeDecorator } from "./ICodeDecorator";
import { CSPROJ } from "../filters";

export class NugetVersionDecorator implements ICodeDecorator
{
Expand All @@ -24,17 +23,18 @@ export class NugetVersionDecorator implements ICodeDecorator
}
});

get filter(): vscode.DocumentSelector {
return CSPROJ;
private readonly regEx: RegExp;

constructor(public readonly filter: vscode.DocumentSelector,tagName: string) {
this.regEx = new RegExp(`<${tagName} Include="(.+)" Version="(.+)"`, "g");
}

async decorate(editor: vscode.TextEditor): Promise<void> {
const text = editor.document.getText();
const okDecorations: vscode.DecorationOptions[] = [];
const newDecorations: vscode.DecorationOptions[] = [];
const regEx = /<PackageReference Include="(.+)" Version="(.+)"/g;
let match;
while ((match = regEx.exec(text))) {
while ((match = this.regEx.exec(text))) {
const id = match[1];
const version = match[2];
const versions = await nuget.searchPackageVersions(editor.document.uri.fsPath, id);
Expand All @@ -50,12 +50,12 @@ export class NugetVersionDecorator implements ICodeDecorator

if (isNew) {
newDecorations.push(decoration);
editor.setDecorations(this.newType, newDecorations);
} else {
okDecorations.push(decoration);
editor.setDecorations(this.okType, okDecorations);
}
}

editor.setDecorations(this.okType, okDecorations);
editor.setDecorations(this.newType, newDecorations);
}
}
4 changes: 3 additions & 1 deletion src/language/filters.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as vscode from "vscode";

export const CSPROJ: vscode.DocumentSelector = { pattern: '**/*.csproj' };
export const CSPROJ: vscode.DocumentSelector = { pattern: '**/*.csproj' };

export const PACKAGES_PROPS: vscode.DocumentSelector = { pattern: '**/*.Packages.props' };

0 comments on commit 157489e

Please sign in to comment.