summaryrefslogtreecommitdiffstats
path: root/devtools/client/projecteditor/lib/plugins/status-bar/plugin.js
blob: 9450baef3b3a66bb3b5605fbb7919ca4b02c0f2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { Cu } = require("chrome");
const { Class } = require("sdk/core/heritage");
const promise = require("promise");
const { registerPlugin, Plugin } = require("devtools/client/projecteditor/lib/plugins/core");

/**
 * Print information about the currently opened file
 * and the state of the current editor
 */
var StatusBarPlugin = Class({
  extends: Plugin,

  init: function () {
    this.box = this.host.createElement("hbox", {
      parent: "#projecteditor-toolbar-bottom"
    });

    this.activeMode = this.host.createElement("label", {
      parent: this.box,
      class: "projecteditor-basic-display"
    });

    this.cursorPosition = this.host.createElement("label", {
      parent: this.box,
      class: "projecteditor-basic-display"
    });

    this.fileLabel = this.host.createElement("label", {
      parent: "#plugin-toolbar-left",
      class: "projecteditor-file-label"
    });
  },

  destroy: function () {
  },

  /**
   * Print information about the current state of the editor
   *
   * @param Editor editor
   */
  render: function (editor, resource) {
    if (!resource || resource.isDir) {
      this.fileLabel.textContent = "";
      this.cursorPosition.value = "";
      return;
    }

    this.fileLabel.textContent = resource.basename;
    this.activeMode.value = editor.toString();
    if (editor.editor) {
      let cursorStart = editor.editor.getCursor("start");
      let cursorEnd = editor.editor.getCursor("end");
      if (cursorStart.line === cursorEnd.line && cursorStart.ch === cursorEnd.ch) {
        this.cursorPosition.value = cursorStart.line + " " + cursorStart.ch;
      } else {
        this.cursorPosition.value = cursorStart.line + " " + cursorStart.ch + " | " +
                                    cursorEnd.line + " " + cursorEnd.ch;
      }
    } else {
      this.cursorPosition.value = "";
    }
  },


  /**
   * Print the current file name
   *
   * @param Resource resource
   */
  onTreeSelected: function (resource) {
    if (!resource || resource.isDir) {
      this.fileLabel.textContent = "";
      return;
    }
    this.fileLabel.textContent = resource.basename;
  },

  onEditorDeactivated: function (editor) {
    this.fileLabel.textContent = "";
    this.cursorPosition.value = "";
  },

  onEditorChange: function (editor, resource) {
    this.render(editor, resource);
  },

  onEditorCursorActivity: function (editor, resource) {
    this.render(editor, resource);
  },

  onEditorActivated: function (editor, resource) {
    this.render(editor, resource);
  },

});

exports.StatusBarPlugin = StatusBarPlugin;
registerPlugin(StatusBarPlugin);