Skip to content

Commit

Permalink
[Fix] Fixed issues from controller template. Fixes #33 (#34)
Browse files Browse the repository at this point in the history
Fixed nonexisting using namespace
Fixed classname (constructor & logger etc)
Fixed error function
  • Loading branch information
KreativJos authored Aug 2, 2021
1 parent ba1cad6 commit 0e90921
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { promises as fs } from 'fs';
import CodeActionProvider from './codeActionProvider';
import NamespaceDetector from './namespaceDetector';

const classnameRegex = new RegExp(/\${classname}/, 'g');
const namespaceRegex = new RegExp(/\${namespace}/, 'g');

export function activate(context: vscode.ExtensionContext): void {
const documentSelector: vscode.DocumentSelector = {
language: 'csharp',
Expand Down Expand Up @@ -92,8 +95,8 @@ async function openTemplateAndSaveNewFile(type: string, namespace: string, filen
const doc = await fs.readFile(templateFilePath, 'utf-8');

let text = doc
.replace('${namespace}', namespace)
.replace('${classname}', filename);
.replace(namespaceRegex, namespace)
.replace(classnameRegex, filename);

const cursorPosition = findCursorInTemplate(text);

Expand Down
7 changes: 3 additions & 4 deletions templates/controller.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MVCProject.Models;

namespace ${namespace}
{
[Route("[controller]")]
public class ${classname} : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly ILogger<${classname}> _logger;

public HomeController(ILogger<HomeController> logger)
public ${classname}(ILogger<${classname}> logger)
{
_logger = logger;${cursor}
}
Expand All @@ -27,7 +26,7 @@ namespace ${namespace}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
return View("Error!");
}
}
}

0 comments on commit 0e90921

Please sign in to comment.