<commit_before>// Copyright (c) 2013, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/**
 * An entrypoint to the [ace.dart](https://github.com/rmsmith/ace.dart) package.
 */
library spark.ace;

import 'dart:async';
import 'dart:html' as html;
import 'dart:js' as js;
import 'dart:math' as math;

import 'package:ace/ace.dart' as ace;
import 'package:path/path.dart' as path;

import 'workspace.dart' as workspace;
import 'editors.dart';
import 'preferences.dart';
import 'utils.dart' as utils;

export 'package:ace/ace.dart' show EditSession;

class AceEditor extends Editor {
  final AceContainer aceContainer;

  workspace.File file;

  html.Element get element => aceContainer.parentElement;

  AceEditor(this.aceContainer);

  void resize() => aceContainer.resize();

  void focus() => aceContainer.focus();
}

/**
 * A wrapper around an Ace editor instance.
 */
class AceContainer {
  static final KEY_BINDINGS = ace.KeyboardHandler.BINDINGS;
  // 2 light themes, 4 dark ones.
  static final THEMES = [
      'textmate', 'tomorrow',
      'tomorrow_night', 'monokai', 'idle_fingers', 'pastel_on_dark'
  ];

  /**
   * The container for the Ace editor.
   */
  final html.Element parentElement;

  ace.Editor _aceEditor;

  static bool get available => js.context['ace'] != null;

  StreamSubscription _markerChangeHandler;

  AceContainer(this.parentElement) {
    _aceEditor = ace.edit(parentElement);
    _aceEditor.renderer.fixedWidthGutter = true;
    _aceEditor.highlightActiveLine = false;
    _aceEditor.printMarginColumn = 80;
    //_aceEditor.setOption('scrollPastEnd', true);
    _aceEditor.readOnly = true;

    // Enable code completion.
    ace.require('ace/ext/language_tools');
    _aceEditor.setOption('enableBasicAutocompletion', true);
    _aceEditor.setOption('enableSnippets', true);

    // Fallback
    theme = THEMES[0];
  }

  List<ace.Annotation> setMarkers(List<workspace.Marker> markers) {
    ace.EditSession currentSession = this.currentSession;
    List<ace.Annotation> annotations = [];

    for (workspace.Marker marker in markers) {
      // TODO(ericarnold): Check the type here before taking from severity.
      String annotationType = _convertMarkerSeverity(marker.severity);
      var annotation = new ace.Annotation(
          text: marker.message,
          row: marker.lineNum,
          type: annotationType);
      annotations.add(annotation);
    }
    currentSession.annotations = annotations;
    return annotations;
  }

  clearAnnotations() => currentSession.annotations = [];

  String get theme => _aceEditor.theme.name;

  set theme(String value) => _aceEditor.theme = new ace.Theme.named(value);

  Future<String> getKeyBinding() {
    var handler = _aceEditor.keyBinding.keyboardHandler;
    return handler.onLoad.then((_) {
      return KEY_BINDINGS.contains(handler.name) ? handler.name : null;
    });
  }

  void setKeyBinding(String name) {
    var handler = new ace.KeyboardHandler.named(name);
    handler.onLoad.then((_) => _aceEditor.keyBinding.keyboardHandler = handler);
  }

  void focus() => _aceEditor.focus();

  void resize() => _aceEditor.resize(false);

  ace.EditSession createEditSession(String text, String fileName) {
    ace.EditSession session = ace.createEditSession(
        text, new ace.Mode.forFile(fileName));
    _applyCustomSession(session, fileName);
    return session;
  }

  void _applyCustomSession(ace.EditSession session, String fileName) {
    String extention = path.extension(fileName);
    switch (extention) {
      case '.dart':
        session.tabSize = 2;
        session.useSoftTabs = true;
        break;
      default:
        // For now, 2-space for all file types by default. This can be changed
        // in the future.
        session.tabSize = 2;
        session.useSoftTabs = true;
        break;
    }
    // Disable Ace's analysis (this shows up in JavaScript files).
    session.useWorker = false;
  }

  html.Point get cursorPosition {
    ace.Point cursorPosition = _aceEditor.cursorPosition;
    return new html.Point(cursorPosition.column, cursorPosition.row);
  }

  void set cursorPosition(html.Point position) {
    _aceEditor.navigateTo(position.y, position.x);
  }

  ace.EditSession get currentSession => _aceEditor.session;

  void switchTo(ace.EditSession session, [workspace.File file]) {
    if (session == null) {
      _aceEditor.session = ace.createEditSession('', new ace.Mode('ace/mode/text'));
      _aceEditor.readOnly = true;
    } else {
      _aceEditor.session = session;

      if (_aceEditor.readOnly) {
        _aceEditor.readOnly = false;
      }
    }

    // Setup the code completion options for the current file type.
    if (file != null) {
      _aceEditor.setOption(
          'enableBasicAutocompletion', path.extension(file.name) != '.dart');
      if (_markerChangeHandler == null) {
        _markerChangeHandler = file.workspace.onMarkerChange.listen(
            _handleMarkerChange);
      }
    }
  }

  void _handleMarkerChange(workspace.MarkerChangeEvent event) {
    setMarkers(event.marker);
  }

  String _convertMarkerSeverity(int markerSeverity) {
    switch (markerSeverity) {
      case workspace.Marker.SEVERITY_ERROR:
        return ace.Annotation.WARNING;
        break;
      case workspace.Marker.SEVERITY_WARNING:
        return ace.Annotation.WARNING;
        break;
      case workspace.Marker.SEVERITY_INFO:
        return ace.Annotation.WARNING;
        break;
    }
  }
}

class ThemeManager {
  AceContainer aceContainer;
  PreferenceStore prefs;
  html.Element _label;

  ThemeManager(this.aceContainer, this.prefs, this._label) {
    prefs.getValue('aceTheme').then((String value) {
      if (value != null) {
        aceContainer.theme = value;
        _updateName(value);
      } else {
        _updateName(aceContainer.theme);
      }
    });
  }

  void inc(html.Event e) {
   e.stopPropagation();
    _changeTheme(1);<commit_msg>Created currentFile instance variable and set it every time the file switches.  Fixed the severity translations. Was mistakenly converting everything to warning.<commit_after>// Copyright (c) 2013, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/**
 * An entrypoint to the [ace.dart](https://github.com/rmsmith/ace.dart) package.
 */
library spark.ace;

import 'dart:async';
import 'dart:html' as html;
import 'dart:js' as js;
import 'dart:math' as math;

import 'package:ace/ace.dart' as ace;
import 'package:path/path.dart' as path;

import 'workspace.dart' as workspace;
import 'editors.dart';
import 'preferences.dart';
import 'utils.dart' as utils;

export 'package:ace/ace.dart' show EditSession;

class AceEditor extends Editor {
  final AceContainer aceContainer;

  workspace.File file;

  html.Element get element => aceContainer.parentElement;

  AceEditor(this.aceContainer);

  void resize() => aceContainer.resize();

  void focus() => aceContainer.focus();
}

/**
 * A wrapper around an Ace editor instance.
 */
class AceContainer {
  static final KEY_BINDINGS = ace.KeyboardHandler.BINDINGS;
  // 2 light themes, 4 dark ones.
  static final THEMES = [
      'textmate', 'tomorrow',
      'tomorrow_night', 'monokai', 'idle_fingers', 'pastel_on_dark'
  ];

  /**
   * The container for the Ace editor.
   */
  final html.Element parentElement;

  ace.Editor _aceEditor;

  static bool get available => js.context['ace'] != null;

  StreamSubscription _markerChangeHandler;
  workspace.File currentFile;

  AceContainer(this.parentElement) {
    _aceEditor = ace.edit(parentElement);
    _aceEditor.renderer.fixedWidthGutter = true;
    _aceEditor.highlightActiveLine = false;
    _aceEditor.printMarginColumn = 80;
    //_aceEditor.setOption('scrollPastEnd', true);
    _aceEditor.readOnly = true;

    // Enable code completion.
    ace.require('ace/ext/language_tools');
    _aceEditor.setOption('enableBasicAutocompletion', true);
    _aceEditor.setOption('enableSnippets', true);

    // Fallback
    theme = THEMES[0];
  }

  List<ace.Annotation> setMarkers(List<workspace.Marker> markers) {
    ace.EditSession currentSession = this.currentSession;
    List<ace.Annotation> annotations = [];

    for (workspace.Marker marker in markers) {
      // TODO(ericarnold): Check the type here before taking from severity.
      String annotationType = _convertMarkerSeverity(marker.severity);
      var annotation = new ace.Annotation(
          text: marker.message,
          row: marker.lineNum,
          type: annotationType);
      annotations.add(annotation);
    }
    currentSession.annotations = annotations;
    return annotations;
  }

  clearAnnotations() => currentSession.annotations = [];

  String get theme => _aceEditor.theme.name;

  set theme(String value) => _aceEditor.theme = new ace.Theme.named(value);

  Future<String> getKeyBinding() {
    var handler = _aceEditor.keyBinding.keyboardHandler;
    return handler.onLoad.then((_) {
      return KEY_BINDINGS.contains(handler.name) ? handler.name : null;
    });
  }

  void setKeyBinding(String name) {
    var handler = new ace.KeyboardHandler.named(name);
    handler.onLoad.then((_) => _aceEditor.keyBinding.keyboardHandler = handler);
  }

  void focus() => _aceEditor.focus();

  void resize() => _aceEditor.resize(false);

  ace.EditSession createEditSession(String text, String fileName) {
    ace.EditSession session = ace.createEditSession(
        text, new ace.Mode.forFile(fileName));
    _applyCustomSession(session, fileName);
    return session;
  }

  void _applyCustomSession(ace.EditSession session, String fileName) {
    String extention = path.extension(fileName);
    switch (extention) {
      case '.dart':
        session.tabSize = 2;
        session.useSoftTabs = true;
        break;
      default:
        // For now, 2-space for all file types by default. This can be changed
        // in the future.
        session.tabSize = 2;
        session.useSoftTabs = true;
        break;
    }
    // Disable Ace's analysis (this shows up in JavaScript files).
    session.useWorker = false;
  }

  html.Point get cursorPosition {
    ace.Point cursorPosition = _aceEditor.cursorPosition;
    return new html.Point(cursorPosition.column, cursorPosition.row);
  }

  void set cursorPosition(html.Point position) {
    _aceEditor.navigateTo(position.y, position.x);
  }

  ace.EditSession get currentSession => _aceEditor.session;

  void switchTo(ace.EditSession session, [workspace.File file]) {
    if (session == null) {
      _aceEditor.session = ace.createEditSession('', new ace.Mode('ace/mode/text'));
      _aceEditor.readOnly = true;
    } else {
      _aceEditor.session = session;

      if (_aceEditor.readOnly) {
        _aceEditor.readOnly = false;
      }
    }

    // Setup the code completion options for the current file type.
    if (file != null) {
      currentFile = file;
      _aceEditor.setOption(
          'enableBasicAutocompletion', path.extension(file.name) != '.dart');

      // TODO(ericarnold): Markers aren't shown until file is edited.  Fix.
      setMarkers(currentFile.getMarkers());

      if (_markerChangeHandler == null) {
        _markerChangeHandler = file.workspace.onMarkerChange.listen(
            _handleMarkerChange);
      }
    }
  }

  void _handleMarkerChange(workspace.MarkerChangeEvent event) {
    // TODO(ericarnold): This gets called repeatedly.  Fix.
    setMarkers(currentFile.getMarkers());
  }

  String _convertMarkerSeverity(int markerSeverity) {
    switch (markerSeverity) {
      case workspace.Marker.SEVERITY_ERROR:
        return ace.Annotation.ERROR;
        break;
      case workspace.Marker.SEVERITY_WARNING:
        return ace.Annotation.WARNING;
        break;
      case workspace.Marker.SEVERITY_INFO:
        return ace.Annotation.INFO;
        break;
    }
  }
}

class ThemeManager {
  AceContainer aceContainer;
  PreferenceStore prefs;
  html.Element _label;

  ThemeManager(this.aceContainer, this.prefs, this._label) {
    prefs.getValue('aceTheme').then((String value) {
      if (value != null) {
        aceContainer.theme = value;
        _updateName(value);
      } else {
        _updateName(aceContainer.theme);
      }
    });
  }

  void inc(html.Event e) {
   e.stopPropagation();
    _changeTheme(1);
{'fraction_non_alphanumeric': 0.10982150004002242, 'fraction_numerical': 0.002081165452653486, 'mean_word_length': 3.236690403526619, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4399136', 'n_tokens_mistral': 3819, 'n_tokens_neox': 3690, 'n_words': 900}