aboutsummaryrefslogtreecommitdiff
path: root/javascript/app/components/identifier-info.js
blob: 0e870a243019f478dd509004bd1afdafd1632b1c (plain) (blame)
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
import Ember from 'ember';
import {goToDefinition} from '../utils/go-to-definition';

export default Ember.Component.extend({  
  store : Ember.inject.service('store'),
  downloadedDocumentation : null,
  didInsertElement () {
    const onmouseup = (event) => {
      if(event.target.dataset.location) {
        let location;
        try {
          location = JSON.parse(event.target.dataset.location);
        } catch (e) {
          console.log(e);
        }
        if(location) {
          goToDefinition(this.get('store'),location,event.which,this.get('currentLineNumber'));
        }
      }
    };
    this.element.addEventListener('mouseup',onmouseup);
    this._onmouseup = onmouseup;
  },
  willDestroyElement : function () {
    if(this._onmouseup) {
      this.element.removeEventListener('mouseup',this._onmouseup);
    }
  },
  //Naughty record selectors : 
  //https://github.com/ghc/ghc/blob/ced2cb5e8fbf4493488d1c336da7b00d174923ce/compiler/typecheck/TcTyDecls.hs#L940-L961
  isNaughtyRecSel : Ember.computed('identifierInfo',function () {
    const idInfo = this.get('identifierInfo');
    return idInfo ? (idInfo.details === "RecSelIdNaughty") : false;   
  }),
  isExternalIdentifier : Ember.computed('identifierInfo',function () {
    const idInfo = this.get('identifierInfo');
    return idInfo ? (idInfo.sort === "External") : false; 
  }),
  identifierObserver : Ember.observer('identifierInfo',function () {
    this.set("downloadedDocumentation","");
    const idInfo = this.get('identifierInfo');
    if(idInfo) {      
      const locationInfo = idInfo.locationInfo;
      if(locationInfo.tag === "ApproximateLocation") {
        const packageId = locationInfo.packageId.name + "-" + locationInfo.packageId.version;
        const currentIdentifier = idInfo;
        
        this.get('store').loadDefinitionSite(packageId,
                                             locationInfo.moduleName,
                                             locationInfo.componentId,
                                             locationInfo.entity,
                                             locationInfo.name)
          .then((definitionSite) => {
            Ember.run.next(this,() => {
              if(currentIdentifier === this.get('identifierInfo')) {                
                this.set('downloadedDocumentation',definitionSite.documentation);
              }})
          }).catch(() => {            
            this.get('store').loadHoogleDocs(packageId,
                                             locationInfo.moduleName,
                                             locationInfo.entity,
                                             locationInfo.name)
              .then((hoogleDocs) => {
                Ember.run.next(this,() => {
                  if(currentIdentifier === this.get('identifierInfo')) {
                    this.set('downloadedDocumentation',hoogleDocs);
                  }});
              });
          });
      }
    }
  })
});