Skip to content

Commit

Permalink
Merge pull request #7 from theia-demo-plugins/editor-api
Browse files Browse the repository at this point in the history
Add usage of Editor API
  • Loading branch information
evidolob authored Jun 11, 2018
2 parents 1d52598 + f404608 commit 772f2d4
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 2 deletions.
112 changes: 112 additions & 0 deletions src/editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2018-2018 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
import * as theia from '@wiptheia/plugin';
export function initEditorsCommands() {

theia.workspace.onDidOpenTextDocument(e => {
console.log(`Text Document opened: ${e.uri}`);
});

theia.workspace.onDidCloseTextDocument( e =>{
console.log(`Text Document closed: ${e.uri}`);
});

theia.workspace.onDidChangeTextDocument( e =>{
if(e.contentChanges.length !== 0){
console.log(`Text Document Changed: 'changes: ${JSON.stringify(e.contentChanges)}', 'document: ${e.document.uri}'`);
}
});

theia.window.onDidChangeActiveTextEditor( e =>{
if(e){
console.log(`Active Editor Changed: ${e.document.uri}`);
} else {
console.log("All editors are closed");
}
});

theia.window.onDidChangeTextEditorOptions( e => {
console.log(`Text Editor options changed: ${e.options}`);
});

theia.window.onDidChangeTextEditorSelection(e => {
if(e.selections.length === 1){
const sel = e.selections[0];
console.log(`Cursor position is: Ln: ${sel.end.line+1}, Col: ${sel.end.character+1}`);
}

});

theia.window.onDidChangeTextEditorVisibleRanges(e => {
console.log(`Text Editor: ${e.textEditor.document.uri} visible ranges: ${JSON.stringify(e.visibleRanges)}`);
});

theia.commands.registerCommand({id: "change editor",label: "Select Editor Cursor"}, ()=>{
theia.window.showQuickPick(["Block","BlockOutline", "Line", "Underline", "LineThin", "UnderlineThin" ],{placeHolder:"Select Cursor"}).then((c: string| undefined) =>{
let cursorStyle: theia.TextEditorCursorStyle = theia.TextEditorCursorStyle.Line;
switch(c!){
case "Block":
cursorStyle = theia.TextEditorCursorStyle.Block;
break;
case "BlockOutline":
cursorStyle = theia.TextEditorCursorStyle.BlockOutline;
break;
case "Line":
cursorStyle = theia.TextEditorCursorStyle.Line;
break;
case "Underline":
cursorStyle = theia.TextEditorCursorStyle.Underline;
break;
case "LineThin":
cursorStyle = theia.TextEditorCursorStyle.LineThin;
break;
case "UnderlineThin":
cursorStyle = theia.TextEditorCursorStyle.UnderlineThin;
break;
}
theia.window.activeTextEditor!.options ={cursorStyle: cursorStyle};
});
});

theia.commands.registerCommand({id: 'reveal range test', label:'Test Reveal Range'}, ()=>{
theia.window.activeTextEditor!.revealRange(new theia.Range(new theia.Position(32, 4), new theia.Position(34,0)), theia.TextEditorRevealType.InCenter);
});

theia.commands.registerCommand({id:'test editor edit',label:'Test EditorEdit'}, ()=>{
const editor = theia.window.activeTextEditor;
if(editor){
editor.edit(edit=>{
edit.insert(new theia.Position(0,0),'Hello from Plugin Editor API');
});
}
});

theia.commands.registerCommand({id:'test editor snippet', label:"Test Editor Snippet"}, ()=>{
const editor = theia.window.activeTextEditor;
if(editor){
editor.insertSnippet(new theia.SnippetString('Hello from ${1|snippet,one,two|},current year: ${CURRENT_YEAR}\n\t${2:some}-Dir:${TM_DIRECTORY}, File:${TM_FILEPATH}$0'));
}
});

theia.commands.registerCommand({id:'test editor decoration', label:'Test Editor Decoration'}, ()=>{
const editor = theia.window.activeTextEditor;
if(editor){
const decoration1 = theia.window.createTextEditorDecorationType({color:'red', textDecoration:'underline overline dotted',cursor:'pointer',});
editor.setDecorations(decoration1, [new theia.Range(0,0,0,9)]);
const decoration2 = theia.window.createTextEditorDecorationType({color:'green'})
editor.setDecorations(decoration2, [{range: new theia.Range(1,0,1,5), hoverMessage:new theia.MarkdownString('Hello From Plugin')}])
setTimeout(()=>{
decoration1.dispose();
}, 5000);
}
});

}
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

import * as theia from '@wiptheia/plugin';
import * as _ from 'lodash';
import {initEditorsCommands} from './editor';
const disposables: theia.Disposable[] = [];

export function start() {
initEditorsCommands();
const command: theia.Command = {
id: 'simple-frontend-plugin-command',
label: 'Command from simple plugin'
Expand Down Expand Up @@ -89,6 +91,7 @@ export function start() {
disposables.push(theia.commands.registerCommand(errorModalMessageTestCommand, (...args: any[]) => {
theia.window.showErrorMessage('Error modal message!', {modal: true});
}));

}

function testQuickPickObject() {
Expand Down Expand Up @@ -120,7 +123,7 @@ function testQuickPickObject() {
}
]);
}, 500);
}), option).then((val: theia.QuickPickItem[] | undefined) => {
}), option).then((val: theia.QuickPickItem| undefined) => {
console.log(`Quick Pick Object Selected: ${JSON.stringify(val)}`);
});
}
Expand All @@ -137,7 +140,7 @@ function testQuickPick(): void {
setTimeout(()=>{
resolve(["foo" + Math.round(Math.random()*10), "bar", "foobar"]);
}, 500);
}), option).then((val: string[] | undefined) => {
}), option).then((val: string | undefined) => {
console.log(`Quick Pick Selected: ${val}`);
});
}
Expand Down

0 comments on commit 772f2d4

Please sign in to comment.