Skip to content

Commit

Permalink
Configurable colors and fixes bug with detect indentation from content.
Browse files Browse the repository at this point in the history
* One can now configure it's own colors.
* Default config does not tamper with detect indentation anymore.
* Added some testfiles for space vs tab indentation.
  • Loading branch information
oderwat committed May 12, 2016
1 parent 968df59 commit 3efd5d5
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Expand Up @@ -10,7 +10,7 @@
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "${workspaceRoot}/out/src",
"outDir": "${workspaceRoot}/out",
"preLaunchTask": "npm"
},
{
Expand Down
1 change: 1 addition & 0 deletions .vscodeignore
Expand Up @@ -2,6 +2,7 @@
typings/**
**/*.ts
**/*.map
tests/**
.gitignore
tsconfig.json
vsc-extension-quickstart.md
16 changes: 14 additions & 2 deletions README.md
Expand Up @@ -25,8 +25,21 @@ Although you can just use it as it is there is the possibility to configure some

*Notice: Defining both `includedLanguages` and `excludedLanguages` does not make much sense. Use one of both!*

You can configure your own colors by adding and tampering with the following code:

The following is experimental and still buggy (do not use it :)
```
// Defining custom colors instead of default "Rainbow" for dark backgrounds.
// (Sorry: Changing them needs an editor restart for now!)
"indentRainbow.colors": [
"rgba(64,64,16,0.3)",
"rgba(32,64,32,0.3)",
"rgba(64,32,64,0.3)",
"rgba(16,48,48,0.3)",
"rgba(128,32,32,0.3)"
]
```

The following is experimental and still buggy. It will basically disable the automatic detection for languages which are not defined in this array. You may not want to use it at all :)

```
// Automatically change indent setting (tabSize / insertSpaces) for a language.
Expand All @@ -39,7 +52,6 @@ The following is experimental and still buggy (do not use it :)
"python": { "tabSize": 4, "insertSpaces": true },
"php": { "tabSize": 4, "insertSpaces": false }
}
```

Build with:
Expand Down
172 changes: 172 additions & 0 deletions extension.js
@@ -0,0 +1,172 @@
"use strict";
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
var vscode = require('vscode');
// this method is called when vs code is activated
function activate(context) {
// create a decorator types that we use to decorate indent levels
var decorationType = [];
var doIt = false;
var clearMe = false;
var currentLanguageId = null;
var activeEditor = vscode.window.activeTextEditor;
var colors = vscode.workspace.getConfiguration('indentRainbow')['color1s'] || [

This comment has been minimized.

Copy link
@Tyriar

Tyriar May 13, 2016

@oderwat looks like a typo here: ['colors']

This comment has been minimized.

Copy link
@oderwat

oderwat May 13, 2016

Author Owner

Eagle Eyes :) But that file has no impact. This is a remainder from me accidentally running it with my code-runner. I added it to the .gitignore and removed it from the repo. Thank you for reporting!

This comment has been minimized.

Copy link
@Tyriar

Tyriar May 13, 2016

👍

"rgba(64,64,16,0.3)",
"rgba(32,64,32,0.3)",
"rgba(64,32,64,0.3)",
"rgba(16,48,48,0.3)",
"rgba(128,32,32,0.9)"
];
for (var i = 0; i < colors.length; i++) {
decorationType[i] = vscode.window.createTextEditorDecorationType({
backgroundColor: colors[i]
});
}
if (activeEditor) {
indentConfig();
}
if (activeEditor && checkLanguage()) {
triggerUpdateDecorations();
}
vscode.window.onDidChangeActiveTextEditor(function (editor) {
activeEditor = editor;
if (editor) {
indentConfig();
}
if (editor && checkLanguage()) {
triggerUpdateDecorations();
}
}, null, context.subscriptions);
vscode.workspace.onDidChangeTextDocument(function (event) {
if (activeEditor) {
indentConfig();
}
if (activeEditor && event.document === activeEditor.document && checkLanguage()) {
triggerUpdateDecorations();
}
}, null, context.subscriptions);
function isEmptyObject(obj) {
return Object.getOwnPropertyNames(obj).length == 0;
}
function indentConfig() {
// Set tabSize and insertSpaces from the config if specified for this languageId
var indentSetter = vscode.workspace.getConfiguration('indentRainbow')['indentSetter'] || [];
// we do nothing if we have {} to not interrupt other extensions for indent settings
if (true || !isEmptyObject(indentSetter)) {
var langCfg = indentSetter[activeEditor.document.languageId];
if (langCfg == undefined) {
// if we do not have any defaults get those from the editor config itself
// this seems to break detectindentation = true :(
langCfg = vscode.workspace.getConfiguration('editor');
}
vscode.window.activeTextEditor.options = {
"tabSize": langCfg.tabSize,
"insertSpaces": langCfg.insertSpaces
};
}
}
function checkLanguage() {
if (activeEditor) {
if (currentLanguageId != activeEditor.document.languageId) {
var inclang = vscode.workspace.getConfiguration('indentRainbow')['includedLanguages'] || [];
var exclang = vscode.workspace.getConfiguration('indentRainbow')['excludedLanguages'] || [];
currentLanguageId = activeEditor.document.languageId;
doIt = true;
if (inclang.length != 0) {
if (inclang.indexOf(currentLanguageId) == -1) {
doIt = false;
}
}
if (doIt && exclang.length != 0) {
if (exclang.indexOf(currentLanguageId) != -1) {
doIt = false;
}
}
}
}
if (clearMe && !doIt) {
// clear decorations when language switches away
var decor = [];
activeEditor.setDecorations(decorationType[0], decor);
activeEditor.setDecorations(decorationType[1], decor);
activeEditor.setDecorations(decorationType[2], decor);
activeEditor.setDecorations(decorationType[3], decor);
activeEditor.setDecorations(decorationType[4], decor);
clearMe = false;
}
indentConfig();
return doIt;
}
var timeout = null;
function triggerUpdateDecorations() {
if (timeout) {
clearTimeout(timeout);
}
var updateDelay = vscode.workspace.getConfiguration('indentRainbow')['updateDelay'] || 100;
timeout = setTimeout(updateDecorations, 100);
}
function updateDecorations() {
if (!activeEditor) {
return;
}
var regEx = /^[\t ]+/gm;
var text = activeEditor.document.getText();
var tabsize = activeEditor.options.tabSize;
var tabs = " ".repeat(tabsize);
var decor0 = [];
var decor1 = [];
var decor2 = [];
var decor3 = [];
var decor4 = [];
var re = new RegExp("\t", "g");
var match;
while (match = regEx.exec(text)) {
var ma = (match[0].replace(re, tabs)).length;
if (ma % tabsize != 0) {
var startPos = activeEditor.document.positionAt(match.index);
var endPos = activeEditor.document.positionAt(match.index + match[0].length);
var decoration = { range: new vscode.Range(startPos, endPos), hoverMessage: null };
decor4.push(decoration);
}
else {
var m = match[0];
var l = m.length;
var o = 0;
var n = 0;
while (n < l) {
var startPos = activeEditor.document.positionAt(match.index + n);
if (m[n] == "\t") {
n++;
}
else {
n += activeEditor.options.tabSize;
}
var endPos = activeEditor.document.positionAt(match.index + n);
var decoration = { range: new vscode.Range(startPos, endPos), hoverMessage: null };
switch (o % 4) {
case 0:
decor0.push(decoration);
break;
case 1:
decor1.push(decoration);
break;
case 2:
decor2.push(decoration);
break;
case 3:
decor3.push(decoration);
break;
}
o++;
}
}
}
activeEditor.setDecorations(decorationType[0], decor0);
activeEditor.setDecorations(decorationType[1], decor1);
activeEditor.setDecorations(decorationType[2], decor2);
activeEditor.setDecorations(decorationType[3], decor3);
activeEditor.setDecorations(decorationType[4], decor4);
clearMe = true;
}
}
exports.activate = activate;
39 changes: 21 additions & 18 deletions extension.ts
Expand Up @@ -6,29 +6,27 @@ import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {

// create a decorator types that we use to decorate indent levels
var decorationType = [
vscode.window.createTextEditorDecorationType({
backgroundColor: 'rgba(64,64,16,0.3)'
}),
vscode.window.createTextEditorDecorationType({
backgroundColor: 'rgba(32,64,32,0.3)'
}),
vscode.window.createTextEditorDecorationType({
backgroundColor: 'rgba(64,32,64,0.3)'
}),
vscode.window.createTextEditorDecorationType({
backgroundColor: 'rgba(16,48,48,0.3)'
}),
vscode.window.createTextEditorDecorationType({
backgroundColor: 'rgba(128,32,32,0.3)'
})];
var decorationType = [];

var doIt = false;
var clearMe = false;
var currentLanguageId = null;

var activeEditor = vscode.window.activeTextEditor;

var colors = vscode.workspace.getConfiguration('indentRainbow')['colors'] || [
"rgba(64,64,16,0.3)",
"rgba(32,64,32,0.3)",
"rgba(64,32,64,0.3)",
"rgba(16,48,48,0.3)",
"rgba(128,32,32,0.3)"
];

for(var i=0; i<colors.length; i++) {
decorationType[i] = vscode.window.createTextEditorDecorationType({
backgroundColor: colors[i] })
}

if(activeEditor) {
indentConfig()
}
Expand Down Expand Up @@ -58,14 +56,19 @@ export function activate(context: vscode.ExtensionContext) {
}
}, null, context.subscriptions);

function isEmptyObject(obj) {
return Object.getOwnPropertyNames(obj).length == 0;
}

function indentConfig() {
// Set tabSize and insertSpaces from the config if specified for this languageId
var indentSetter = vscode.workspace.getConfiguration('indentRainbow')['indentSetter'] || {};
var indentSetter = vscode.workspace.getConfiguration('indentRainbow')['indentSetter'] || [];
// we do nothing if we have {} to not interrupt other extensions for indent settings
if( indentSetter != {} ) {
if(! isEmptyObject(indentSetter) ) {
var langCfg = indentSetter[ activeEditor.document.languageId ];
if( langCfg == undefined ) {
// if we do not have any defaults get those from the editor config itself
// this seems to break detectindentation = true :(
langCfg = vscode.workspace.getConfiguration('editor');
}
vscode.window.activeTextEditor.options = {
Expand Down
Binary file removed indent-rainbow-0.3.2.vsix
Binary file not shown.

0 comments on commit 3efd5d5

Please sign in to comment.