Skip to content

Commit

Permalink
Support multiple language mappings
Browse files Browse the repository at this point in the history
This will store running kernels by the language from their kernel spec.
It also makes sure that all names are normalized to lower case to avoid confusion.

Fixes #684
  • Loading branch information
lgeiger authored and rgbkrk committed Apr 2, 2017
1 parent 1e88572 commit ec2d518
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
9 changes: 3 additions & 6 deletions lib/kernel-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,12 @@ class KernelManager {
});
}

kernelSpecProvidesLanguage(kernelSpec, language) {
kernelSpecProvidesLanguage(kernelSpec, grammarLanguage) {
const kernelLanguage = kernelSpec.language;
const mappedLanguage = Config.getJson("languageMappings")[kernelLanguage];

if (mappedLanguage) {
return mappedLanguage === language;
}

return kernelLanguage.toLowerCase() === language;
return mappedLanguage === grammarLanguage ||
kernelLanguage === grammarLanguage;
}

getKernelSpecsFromSettings() {
Expand Down
5 changes: 2 additions & 3 deletions lib/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Emitter } from "atom";
import { observable, action } from "mobx";

import { grammarToLanguage, log } from "./utils";
import { log } from "./utils";
import store from "./store";

import WatchSidebar from "./watch-sidebar";
Expand Down Expand Up @@ -31,8 +31,7 @@ export default class Kernel {
this.grammar = grammar;
this.watchSidebar = new WatchSidebar(this);

// $FlowFixMe Since grammar is defined, grammarToLanguage will return a string
this.language = grammarToLanguage(grammar);
this.language = kernelSpec.language.toLowerCase();
this.displayName = kernelSpec.display_name;
}

Expand Down
12 changes: 11 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import _ from "lodash";
import os from "os";
import path from "path";

import Config from "./config";
import store from "./store";

export function reactFactory(
Expand All @@ -25,7 +26,16 @@ export function reactFactory(
}

export function grammarToLanguage(grammar: ?atom$Grammar) {
return grammar ? grammar.name.toLowerCase() : null;
if (!grammar) return null;
const grammarLanguage = grammar.name.toLowerCase();

const mappings = Config.getJson("languageMappings");
const kernelLanguage = _.findKey(
mappings,
l => l.toLowerCase() === grammarLanguage
);

return kernelLanguage ? kernelLanguage.toLowerCase() : grammarLanguage;
}

const markupGrammars = new Set([
Expand Down
28 changes: 24 additions & 4 deletions spec/utils-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,30 @@ import {
} from "./../lib/utils";

describe("utils", () => {
it("grammarToLanguage", () => {
expect(grammarToLanguage({ name: "Kernel Name" })).toEqual("kernel name");
expect(grammarToLanguage(null)).toBeNull();
expect(grammarToLanguage(undefined)).toBeNull();
describe("grammarToLanguage", () => {
it("should return null if no rammar given", () => {
expect(grammarToLanguage()).toBeNull();
expect(grammarToLanguage(null)).toBeNull();
expect(grammarToLanguage(undefined)).toBeNull();
});

it("should return language from grammar", () => {
expect(grammarToLanguage({ name: "Kernel Name" })).toEqual("kernel name");
});

it("should return respect languageMappings", () => {
atom.config.set(
"Hydrogen.languageMappings",
`{"Kernel Language": "Grammar Language"}`
);
expect(grammarToLanguage({ name: "Grammar Language" })).toEqual(
"kernel language"
);
expect(grammarToLanguage({ name: "Kernel Language" })).toEqual(
"kernel language"
);
atom.config.set("Hydrogen.languageMappings", "");
});
});

it("reactFactory", () => {
Expand Down

0 comments on commit ec2d518

Please sign in to comment.