atom分析-項目目錄中的git狀態顯示

    最近都在用atom來代替了sublime text來寫代碼,github強大的開源插件讓這個編輯器變得很是的強大並且有個性化,推薦前端開發的朋友都去嘗試使用,爲何不是都建議使用呢?由於atom佔用的內存比sublime text確實大了不少不少,目前還出現過崩潰的狀況。javascript

    此次不是討論atom的好壞,而是發現了一個問題,atom不能顯示git項目的分支。前端

    用atom的朋友應該都都知道,atom打開了git項目(不須要是github),打開項目的其中一個文件,那麼在atom的右下角位置都會顯示你項目當前的分支,顯示修改了多少行,刪除了多少行這樣的狀態,以下圖java

可是發現有時候,打開了git項目卻沒有像上面那樣的顯示,讓我一度懷疑本身的atom已經壞了......react

通過試驗發現,atom沒有我想象中的那麼智能吧,這種狀況下添加項目文件是沒有的git

而這樣添加項目文件卻可以正確顯示了,以下es6

觀察能夠看出來,這二者的區別在於第一種狀況是git項目在添加項目目錄的時候是二級目錄,而第二種狀況則是一級目錄,那就是說atom只能識別項目的一級目錄?github

這個時候應該確認一下atom是若是讀取git項目的,那麼須要打開atom的調試器(windows下的快捷鍵是alt+ctrl+i),atom編輯器調試的對象是atom,那麼分析它讀取的項目是atom.project,往下查看發現有一個讀取項目的倉庫方法 getRepositories,因此在調試工具裏面執行windows

atom.project.getRepositories

以下圖數組

  如上圖點擊操做,找到了這個方法的源碼,babel

Project.prototype.getRepositories = function() {
      return this.repositories;
    };

得知,直接返回的是 repositories 這個屬性,那麼接着往下找,搜索關鍵詞(ctrl+f) repositories,找到這個相關的設置,發現這麼一段代碼

Project.prototype.setPaths = function(projectPaths) {
      var projectPath, repository, _i, _j, _len, _len1, _ref1;
      _ref1 = this.repositories;
      for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
        repository = _ref1[_i];
        if (repository != null) {
          repository.destroy();
        }
      }
      this.rootDirectories = [];
      this.repositories = [];
      for (_j = 0, _len1 = projectPaths.length; _j < _len1; _j++) {
        projectPath = projectPaths[_j];
        this.addPath(projectPath, {
          emitEvent: false
        });
      }
      return this.emitter.emit('did-change-paths', projectPaths);
    };

" this.repositories = []; "這個是初始化設置爲空數組,往下的代碼就是進行設置各個項目路徑,體檢路徑是在 " this.addPath " 中,因此找到了addPath的方法,以下

Project.prototype.addPath = function(projectPath, options) {
      var directory, directoryExists, provider, repo, rootDirectory, _i, _j, _k, _len, _len1, _len2, _ref1, _ref2, _ref3;
      directory = null;
      _ref1 = this.directoryProviders;
      for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
        provider = _ref1[_i];
        if (directory = typeof provider.directoryForURISync === "function" ? provider.directoryForURISync(projectPath) : void 0) {
          break;
        }
      }
      if (directory == null) {
        directory = this.defaultDirectoryProvider.directoryForURISync(projectPath);
      }
      directoryExists = directory.existsSync();
      _ref2 = this.getDirectories();
      for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
        rootDirectory = _ref2[_j];
        if (rootDirectory.getPath() === directory.getPath()) {
          return;
        }
        if (!directoryExists && rootDirectory.contains(directory.getPath())) {
          return;
        }
      }
      this.rootDirectories.push(directory);
      repo = null;
      _ref3 = this.repositoryProviders;
      for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) {
        provider = _ref3[_k];
        if (repo = typeof provider.repositoryForDirectorySync === "function" ? provider.repositoryForDirectorySync(directory) : void 0) {
          break;
        }
      }
      this.repositories.push(repo != null ? repo : null);
      if ((options != null ? options.emitEvent : void 0) !== false) {
        return this.emitter.emit('did-change-paths', this.getPaths());
      }
    };

在 "  this.repositories.push  "進行斷點,F5從新加載atom,觀察,發現若是git項目在二級目錄的狀況是這樣的

而git項目是一級目錄的狀況是這樣的

因而可知,atom確實是只能識別項目的一級目錄(若是分析setPaths這個方法也會發現atom實際也是讀取一級目錄)。固然,這裏調試看到的實際上是編譯後的代碼,那麼這個文件相關的源碼是在  https://github.com/atom/atom/blob/master/src/project.coffee 這裏,而atom相關的源碼是在 https://github.com/atom/atom/tree/master/src 這裏,由於其中還包換了若是讀取git項目狀態等方法,這裏就不討論了,有興趣的自個去看看,研究,若是有什麼心得就跟大夥分享一下。

另外推薦幾個插件:

simplified-chinese-menu -- 簡體中文漢化包(像我這樣不懂英文的人必備)。

file-icons --很是漂亮的文件圖標擴展,當你往項目目錄一看的時候,簡直不能再爽了。

atom-beautify --不爲啥,就爲了格式化代碼,必備。

language-babel --這個是跟babel相關的,寫es6 react必備的插件。

另外想說的,想我這樣菜鳥的朋友,遇到問題認真分析,總能夠找到你要的答案。

相關文章
相關標籤/搜索