Skip to content

Commit

Permalink
Merge pull request #3 from d1820/bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
d1820 authored Jan 20, 2024
2 parents a697759 + a999e9b commit a2f6a21
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 54 deletions.
12 changes: 0 additions & 12 deletions Sample/SampleProject/BaseClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,5 @@ namespace Sample
{
public class BaseClass : IBaseClass
{
public string FullPropertyAlt
{
get
{
return _fullProperty;
}
set
{
_fullProperty = value;
}
}
public int MyProperty { get; set; }
}
}
12 changes: 12 additions & 0 deletions Sample/SampleProject/MyClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ public string FullProperty
get => _fullProperty;
set => _fullProperty = value;
}
public string FullPropertyAlt
{
get
{
return _fullProperty;
}
set
{
_fullProperty = value;
}
}
public int MyProperty { get; set; }
public async Task<int> GetNewIdAsync<TNewType>(string name,
string address,
string city,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "DanTurco",
"displayName": "PullToInterfacor",
"description": "A Visual Studio Code Extension to include the ability to **Pull** methods and properties to inherited interfaces and base classes. This is targeted to C# development and is meant as a supplemental extension to C# Dev Kit. This extension supports pulling public properties and methods to interfaces, and public and protected methods to base classes.",
"version": "1.0.0",
"version": "1.0.1",
"engines": {
"vscode": "^1.85.0"
},
Expand Down Expand Up @@ -46,7 +46,7 @@
"lint": "eslint src --ext ts",
"lint-fix": "eslint src --ext ts --fix",
"test": "vscode-test",
"test-jest": "jest",
"test-jest": "jest --verbose false",
"test-jest-watch": "jest --watch",
"test-jest-coverage": "jest --coverage",
"test:integration": "npm run compile && node ./node_modules/vscode/bin/test",
Expand Down
39 changes: 25 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { WorkspaceFolder } from 'vscode';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import { getWorkspaceFolder } from './utils/workspace-util';
import * as csharp from './pull-to-interface-csharp';
import { IWindow } from './interfaces/window.interface';
import { SignatureLineResult, SignatureType, checkIfAlreadyPulledToInterface, cleanExcessiveNewLines, getLineEnding, getMemberBodyByBrackets, getMemberBodyBySemiColon, getMemberName, getUsingStatements } from './utils/csharp-util';
import { SignatureLineResult, SignatureType, addLineBetweenMembers, checkIfAlreadyPulledToInterface, formatTextWithProperNewLines, getLineEnding, getMemberBodyByBrackets, getMemberBodyBySemiColon, getMemberName, getUsingStatements } from './utils/csharp-util';


const extensionName = 'pulltointerfacor.pullto';
Expand All @@ -15,9 +14,9 @@ const extensionName = 'pulltointerfacor.pullto';
export async function activate(context: vscode.ExtensionContext)
{
var wsf = vscode.workspace.workspaceFolders;
const workspaceRoot: string = getWorkspaceFolder(wsf as WorkspaceFolder[]);
const workspaceRoot: string = getWorkspaceFolder(wsf as vscode.WorkspaceFolder[]);

let disposable = vscode.commands.registerTextEditorCommand(extensionName, async (editor) =>
let disposable = vscode.commands.registerTextEditorCommand(extensionName, async (editor: vscode.TextEditor) =>
{
if (editor && editor.document.languageId === 'csharp')
{
Expand Down Expand Up @@ -54,7 +53,7 @@ const buildSubCommands = async (subcommands: string[], context: vscode.Extension
const isRegistered = await isSubcommandRegisteredAsync(subCommandName);
if (!isRegistered)
{
const disposable = vscode.commands.registerTextEditorCommand(subCommandName, async (editor) =>
const disposable = vscode.commands.registerTextEditorCommand(subCommandName, async (editor: vscode.TextEditor) =>
{
const signatureResult = csharp.getSignatureToPull(editor, '(public|protected)');
let methodBodySignature: SignatureLineResult | null = null;
Expand All @@ -65,14 +64,19 @@ const buildSubCommands = async (subcommands: string[], context: vscode.Extension
vscode.window.showErrorMessage(`Unsupported pull. Unable to determine what to pull. 'public' properties and 'public' or 'protected' methods are only supported. Please copy manually`);
return;
}

const tokenSource = new vscode.CancellationTokenSource();
//read file contents
const files = await vscode.workspace.findFiles(`**/${subcommand}.cs`, '**/node_modules/**');
if (files.length > 1)
{
vscode.window.showErrorMessage(`More then one file found matching ${subcommand}. Please copy manually`);
return;
}
if (files.length === 0)
{
vscode.window.showErrorMessage(`No files found matching ${subcommand}. Please copy manually`);
return;
}
const eol = getLineEnding(editor);
const selectedFileDocument = await vscode.workspace.openTextDocument(files[0].path);
let selectedFileDocumentContent = selectedFileDocument.getText();
Expand Down Expand Up @@ -118,10 +122,10 @@ const buildSubCommands = async (subcommands: string[], context: vscode.Extension

if (selectedFileDocumentContent)
{
const currentDocumentUsings = getUsingStatements(editor);
const currentDocumentUsings = getUsingStatements(editor, eol);
selectedFileDocumentContent = csharp.addUsingsToDocument(eol, selectedFileDocumentContent, currentDocumentUsings);
selectedFileDocumentContent = cleanExcessiveNewLines(selectedFileDocumentContent, eol);

selectedFileDocumentContent = formatTextWithProperNewLines(selectedFileDocumentContent, eol);
selectedFileDocumentContent = addLineBetweenMembers(selectedFileDocumentContent,eol);
const success = await csharp.applyEditsAsync(files[0].path, selectedFileDocumentContent);
if (!success)
{
Expand All @@ -138,12 +142,9 @@ const buildSubCommands = async (subcommands: string[], context: vscode.Extension
if (!subcommand.startsWith("I") && methodBodySignature?.signature)
{
//remove if from current file
const activeFileUrl = editor.document.uri;
const activeFileUrl = editor.document.uri.path;
const currentFileDocument = await vscode.workspace.openTextDocument(activeFileUrl);
let currentFileDocumentContent = currentFileDocument.getText();
currentFileDocumentContent = currentFileDocumentContent.replace(methodBodySignature.signature + eol, '');
currentFileDocumentContent = cleanExcessiveNewLines(currentFileDocumentContent, eol);
const success = await csharp.applyEditsAsync(activeFileUrl.path, currentFileDocumentContent);
const success = await removeFromCurrentEditorAsync(currentFileDocument, methodBodySignature, eol, activeFileUrl);
if (!success)
{
vscode.window.showErrorMessage(`Unable to remove ${methodBodySignature.signatureType}. Please remove manually`);
Expand Down Expand Up @@ -179,6 +180,16 @@ const buildSubCommands = async (subcommands: string[], context: vscode.Extension
}
};

async function removeFromCurrentEditorAsync(currentFileDocument: vscode.TextDocument, methodBodySignature: SignatureLineResult, eol: string, activeFileUrl: string)
{
let currentFileDocumentContent = currentFileDocument.getText();
currentFileDocumentContent = currentFileDocumentContent.replace(methodBodySignature.signature + eol, '');
currentFileDocumentContent = formatTextWithProperNewLines(currentFileDocumentContent, eol);
currentFileDocumentContent = addLineBetweenMembers(currentFileDocumentContent,eol);
const success = await csharp.applyEditsAsync(activeFileUrl, currentFileDocumentContent);
return success;
}

// This method is called when your extension is deactivated
export function deactivate()
{ }
6 changes: 5 additions & 1 deletion src/pull-to-interface-csharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export const getSubCommandsAsync = async (workspaceRoot: string, window: IWindow
const openFileAndReadInheritedNamesAsync = async (fileName: string, tracker: InheritedMemberTracker): Promise<string[]> =>
{
const files = await workspace.findFiles(`**/${fileName}.cs`, '**/node_modules/**');
if (files.length > 1 || files.length === 0)
{
return [];
}
const document = await workspace.openTextDocument(files[0].path);
const text = document.getText();
const inheritedNames = getInheritedNames(text, true);
Expand Down Expand Up @@ -167,7 +171,7 @@ export const addUsingsToDocument = (
return documentFileContent;
}
//add the usings to file content
const existingDocumentUsings = getUsingStatementsFromText(documentFileContent);
const existingDocumentUsings = getUsingStatementsFromText(documentFileContent, eol);
let combinedUsings = [...usings, ...existingDocumentUsings];
combinedUsings = [...new Set(combinedUsings)]; //distinct
documentFileContent = replaceUsingStatementsFromText(documentFileContent, combinedUsings, eol);
Expand Down
156 changes: 156 additions & 0 deletions src/test/test-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,159 @@ namespace Sample
}
}
`;


export const testTextWithProperNewLines = `using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Sample
{
public class MyClass<TType> : BaseClass, IMyClass, IMyTypedClass<string> where TType : class
{
private string fullProperty;
public int MyProperty { get; set; }
public int MyPropertyLamda => 5;
public string FullProperty
{
get => fullProperty;
set => fullProperty = value;
}
public string FullPropertyAlt
{
get
{
return fullProperty;
}
set
{
fullProperty = value;
}
}
}
}
`;

export const testTextWithProperNewLinesExpected = `using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Sample
{
public class MyClass<TType> : BaseClass, IMyClass, IMyTypedClass<string> where TType : class
{
private string fullProperty;
public int MyProperty { get; set; }
public int MyPropertyLamda => 5;
public string FullProperty
{
get => fullProperty;
set => fullProperty = value;
}
public string FullPropertyAlt
{
get
{
return fullProperty;
}
set
{
fullProperty = value;
}
}
}
}
`;





export const testAddLinesBetweenMembers = `using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Sample
{
public class MyClass<TType> : BaseClass, IMyClass, IMyTypedClass<string> where TType : class
{
private string fullProperty;
public int MyProperty { get; set; }
public int MyPropertyLamda => 5;
public string FullProperty
{
get => fullProperty;
set => fullProperty = value;
}
public string FullPropertyAlt
{
get
{
return fullProperty;
}
set
{
fullProperty = value;
}
}
}
}
`;

export const testAddLinesBetweenMembersExpected = `using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Sample
{
public class MyClass<TType> : BaseClass, IMyClass, IMyTypedClass<string> where TType : class
{
private string fullProperty;
public int MyProperty { get; set; }
public int MyPropertyLamda => 5;
public string FullProperty
{
get => fullProperty;
set => fullProperty = value;
}
public string FullPropertyAlt
{
get
{
return fullProperty;
}
set
{
fullProperty = value;
}
}
}
}
`;
Loading

0 comments on commit a2f6a21

Please sign in to comment.