Skip to content

Commit

Permalink
Merge pull request #141 from pajoma/136-disable-syntax-highlighting-e…
Browse files Browse the repository at this point in the history
…ntirely

Making syntax highlighting configurable
  • Loading branch information
pajoma committed Sep 12, 2022
2 parents 6662c62 + e2bbfcb commit de463f3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Rework the whole syntax highlighting (it's a mess)
* More code actions and code lenses (e.g. to pull all open tasks to the current journal entry)

## 1.0.2
* [Issue #136](https://github.com/pajoma/vscode-journal/issues/136) Added a configuration option to disable syntax highlighting (disabled by default)
## 1.0.1
* Smaller Bugfixes from release 1.0.0
* A few tweaks for language support
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@
"default": "",
"description": "The locale to use (required for the date format). Defaults to the language setting from Visual Studio Code"
},
"journal.syntax-highlighting": {
"type": "boolean",
"default": "false",
"description": "Enable extension specific syntax highlighting (default is false)"
},
"journal.patterns": {
"type": "object",
"editPresentation": "multilineText",
Expand Down
10 changes: 8 additions & 2 deletions src/ext/conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type ScopeDefinition = {
export class Configuration {



private patterns: Map<string, ScopedTemplate> = new Map();


Expand Down Expand Up @@ -174,14 +175,19 @@ export class Configuration {
}

let ext: string | undefined = this.config.get<string>('ext');
ext = (isNullOrUndefined(ext) && (ext!.length === 0)) ? 'md' : ext;
ext = (isNullOrUndefined(ext) && (ext!.length === 0)) ? 'md' : ext!;

if (ext!.startsWith(".")) { ext = ext!.substring(1, ext!.length); }
if (ext.startsWith(".")) { ext = ext.substring(1, ext.length); }

return ext!;
}


public isSyntaxHighlightingEnabled(): boolean {
let result = this.config.get<boolean>("syntax-highlighting");
return (isNullOrUndefined(result)) ? false : result!;
}

/**
* Configuration for the path, where the notes are to be placed
*
Expand Down
50 changes: 46 additions & 4 deletions src/ext/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import * as vscode from 'vscode';
import * as J from '..';
import * as Path from 'path';
import * as fs from 'fs';
import { TextMateRule } from '../model/vscode';
import { isNullOrUndefined } from '../util';

export class Startup {

Expand Down Expand Up @@ -134,7 +136,7 @@ export class Startup {
}

}

public async registerCodeActions(ctrl: J.Util.Ctrl, context: vscode.ExtensionContext): Promise<void> {
try {

Expand All @@ -154,7 +156,7 @@ export class Startup {
}


getConfiguration() : J.Extension.Configuration {
getConfiguration(): J.Extension.Configuration {
return this.ctrl.config;
}

Expand All @@ -164,6 +166,44 @@ export class Startup {
}


public registerSyntaxHighlighting(ctrl: J.Util.Ctrl): Promise<J.Util.Ctrl> {
return new Promise<J.Util.Ctrl>((resolve, reject) => {
if (this.ctrl.config.isSyntaxHighlightingEnabled()) {
return this.enableSyntaxHighlighting(ctrl);
} else { return this.disableSyntaxHighlighting(ctrl); }


});
}


public disableSyntaxHighlighting(ctrl: J.Util.Ctrl): Promise<J.Util.Ctrl> {


return new Promise<J.Util.Ctrl>((resolve, reject) => {
try {
let tokenColorCustomizations: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('editor.tokenColorCustomizations');
if (!tokenColorCustomizations.has("textMateRules")) { resolve(ctrl); }

const rules: TextMateRule[] = tokenColorCustomizations.get<TextMateRule[]>("textMateRules")!;
let result: TextMateRule[] = new Array();
rules.forEach(rule => {
if (!rule.scope.includes("journal")) {
result.push(rule);
}
});


// overwrite config with new config
vscode.workspace.getConfiguration().update("editor.tokenColorCustomizations", { "textMateRules" : result }, vscode.ConfigurationTarget.Global).then(() => resolve(ctrl));
} catch (error) {
reject(error);
}


});
}


/**
* Sets default syntax highlighting settings on startup, we try to differentiate between dark and light themes
Expand All @@ -173,7 +213,7 @@ export class Startup {
* @returns {Q.Promise<J.Util.Ctrl>}
* @memberof Startup
*/
public registerSyntaxHighlighting(ctrl: J.Util.Ctrl): Promise<J.Util.Ctrl> {
public enableSyntaxHighlighting(ctrl: J.Util.Ctrl): Promise<J.Util.Ctrl> {

return new Promise<J.Util.Ctrl>((resolve, reject) => {

Expand All @@ -188,8 +228,10 @@ export class Startup {


let tokenColorCustomizations: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('editor.tokenColorCustomizations');

const rules: TextMateRule[] | undefined = tokenColorCustomizations.get<TextMateRule[]>("textMateRules");

if (tokenColorCustomizations.has("textMateRules")) {
if (isNullOrUndefined(rules) || rules!.length > 0) {
// user customized the section, we do nothing
resolve(ctrl);
}
Expand Down
6 changes: 6 additions & 0 deletions src/model/vscode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AnyARecord } from 'dns';
import * as vscode from 'vscode';
import * as J from '..';

Expand All @@ -12,4 +13,9 @@ export interface DecoratedQuickPickItem extends vscode.QuickPickItem {
path: string;
pickItem?: J.Model.JournalPageType;
fileEntry?: J.Model.FileEntry;
}

export interface TextMateRule {
scope: string;
settings: any;
}

0 comments on commit de463f3

Please sign in to comment.