Skip to content

Commit 70c4a8e

Browse files
committed
Merge from master and refactor silent logging
2 parents f38ff08 + 5e81d09 commit 70c4a8e

9 files changed

Lines changed: 106 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
66

77
## [Unreleased]
88

9-
## 1.1.0 - 2017-05-09
109

10+
## 1.1.1 - 2017-05-27
11+
### Fixed
12+
- Issue #10: Don't show format failure messages when using the `Format Document` from VsCode.
13+
14+
## 1.1.0 - 2017-05-09
15+
### Added
1116
- Issue #6: Formatting when there's only a single table in the entire file.
1217
- Issue #4: Add support for CJK characters.
1318

1419
## 1.0.1 - 2017-04-08
15-
20+
### Fixed
1621
- Fixed issue #1 by improving the detection of header separator to avoid unintended table formatting failures.
1722

1823
## 1.0.0 - 2017-04-03
19-
2024
### Added
21-
22-
- Support to format individual tables with right click -> format selection.
25+
- Support to format individual tables with right click -> `Format Selection`.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "markdown-table-prettify",
33
"displayName": "Markdown Table Prettifier",
44
"description": "Transforms markdown tables to be more readable.",
5-
"version": "1.1.0",
5+
"version": "1.1.1",
66
"publisher": "darkriszty",
77
"repository": {
88
"type": "git",

src/diagnostics/baseLogger.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ILogger } from "./logger";
2+
3+
export abstract class BaseLogger implements ILogger {
4+
private _enabled: boolean = true;
5+
6+
public setEnabled(enabled: boolean): void {
7+
this._enabled = enabled;
8+
}
9+
10+
public abstract logInfo(message: string): void;
11+
public abstract logError(error: string | Error): void;
12+
13+
protected logIfEnabled(logFunc: (any) => void, param: string | Error): void {
14+
if (!this._enabled) return;
15+
logFunc(param);
16+
}
17+
}

src/diagnostics/consoleLogger.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { ILogger } from "./logger";
2+
import { BaseLogger } from "./baseLogger";
23

3-
export class ConsoleLogger implements ILogger {
4+
export class ConsoleLogger extends BaseLogger implements ILogger {
45

5-
logInfo(message: string): void {
6-
console.log(message);
6+
public logInfo(message: string): void {
7+
super.logIfEnabled(console.log, message);
78
}
89

9-
logError(error: string | Error): void {
10-
console.error(error);
10+
public logError(error: string | Error): void {
11+
super.logIfEnabled(console.error, error);
1112
}
1213
}

src/diagnostics/logger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface ILogger {
2+
setEnabled(enabled: boolean): void;
23
logInfo(message: string): void;
34
logError(error: Error | string): void;
45
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as vscode from "vscode";
2+
import { ILogger } from "../diagnostics/logger";
3+
4+
export class SelectionBasedLogToogler {
5+
constructor(
6+
private readonly _document: vscode.TextDocument,
7+
private readonly _range: vscode.Range) { }
8+
9+
public toogleLoggers(loggers: ILogger[]) {
10+
const logEnabled = !this._isWholeDocumentFormatting();
11+
for (const logger of loggers) {
12+
logger.setEnabled(logEnabled);
13+
}
14+
}
15+
16+
private _isWholeDocumentFormatting(): boolean {
17+
if (this._document.lineCount < 1)
18+
return true;
19+
20+
const zeroPosition = new vscode.Position(0, 0);
21+
const documentEndPosition = this._document.lineAt(this._document.lineCount - 1).range.end;
22+
if (this._range.start.isEqual(zeroPosition) && this._range.end.isEqual(documentEndPosition))
23+
return true;
24+
25+
return false;
26+
}
27+
}

src/diagnostics/vsWindowLogger.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import * as vscode from 'vscode';
22
import { ILogger } from "./logger";
3+
import { BaseLogger } from './baseLogger';
34

4-
export class VsWindowLogger implements ILogger {
5+
export class VsWindowLogger extends BaseLogger implements ILogger {
56

6-
logInfo(message: string): void {
7-
vscode.window.showInformationMessage(message);
7+
public logInfo(message: string): void {
8+
super.logIfEnabled(vscode.window.showInformationMessage, message);
89
}
910

10-
logError(error: string | Error): void {
11+
public logError(error: string | Error): void {
1112
const message: string = error instanceof Error
1213
? (<Error>error).message
1314
: error;
14-
15-
vscode.window.showErrorMessage(message);
15+
super.logIfEnabled(vscode.window.showErrorMessage, message);
1616
}
1717
}

src/extension/tableRangePrettyfier.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from "vscode";
22
import { ILogger } from "../diagnostics/logger";
3+
import { SelectionBasedLogToogler } from "../diagnostics/selectionBasedLogToogler";
34
import { Table } from "../models/table";
45
import { TableFactory } from "../modelFactory/tableFactory";
56
import { TableValidator } from "../modelFactory/tableValidator";
@@ -17,14 +18,16 @@ export class TableRangePrettyfier implements vscode.DocumentRangeFormattingEditP
1718
private _loggers: ILogger[]
1819
) { }
1920

20-
provideDocumentRangeFormattingEdits(
21+
public provideDocumentRangeFormattingEdits(
2122
document: vscode.TextDocument, range: vscode.Range,
2223
options: vscode.FormattingOptions, token: vscode.CancellationToken) : vscode.TextEdit[]
2324
{
2425
const result: vscode.TextEdit[] = [];
2526
const selection = document.getText(range);
2627

28+
this.toogleLogging(document, range);
2729
let message: string = null;
30+
2831
try {
2932
if (this._tableValidator.isValid(selection)) {
3033
const table: Table = this._tableFactory.getModel(selection);
@@ -43,4 +46,9 @@ export class TableRangePrettyfier implements vscode.DocumentRangeFormattingEditP
4346

4447
return result;
4548
}
49+
50+
private toogleLogging(document: vscode.TextDocument, range: vscode.Range) {
51+
const toogler = new SelectionBasedLogToogler(document, range);
52+
toogler.toogleLoggers(this._loggers);
53+
}
4654
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as assert from "assert";
2+
import * as vscode from "vscode";
3+
import { IMock, Mock, It, Times } from "typemoq";
4+
import { SelectionBasedLogToogler } from "../../../src/diagnostics/selectionBasedLogToogler";
5+
import { MarkdownTextDocumentStub } from "../../stubs/markdownTextDocumentStub";
6+
import { ILogger } from "../../../src/diagnostics/logger";
7+
8+
suite("SelectionBasedLogToogler tests", () => {
9+
10+
test("toogleLoggers() with entire document disables all loggers", () => {
11+
const loggerMocks: IMock<ILogger>[] = [ Mock.ofType<ILogger>(), Mock.ofType<ILogger>() ];
12+
const document = new MarkdownTextDocumentStub("test");
13+
const sut = new SelectionBasedLogToogler(document, document.getFullRange());
14+
15+
sut.toogleLoggers(loggerMocks.map(_ => _.object));
16+
17+
loggerMocks.forEach(m => m.verify(_ => _.setEnabled(false), Times.once()));
18+
});
19+
20+
test("toogleLoggers() with document fagment enables all loggers", () => {
21+
const loggerMocks: IMock<ILogger>[] = [ Mock.ofType<ILogger>(), Mock.ofType<ILogger>() ];
22+
const document = new MarkdownTextDocumentStub(
23+
`test
24+
test`);
25+
const sut = new SelectionBasedLogToogler(document, document.getRangeForLines(1, 1));
26+
27+
sut.toogleLoggers(loggerMocks.map(_ => _.object));
28+
29+
loggerMocks.forEach(m => m.verify(_ => _.setEnabled(true), Times.once()));
30+
});
31+
});

0 commit comments

Comments
 (0)