Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial nix language support #1911

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions data/playground/nix/asserts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# https://nixos.org/manual/nix/stable/language/constructs.html#assertions

{ localServer ? false
, httpServer ? false
, sslSupport ? false
, pythonBindings ? false
, javaSwigBindings ? false
, javahlBindings ? false
, stdenv, fetchurl
, openssl ? null, httpd ? null, db4 ? null, expat, swig ? null, j2sdk ? null
}:

assert localerver -> db4 != null;
assert httpServer -> httpd != null && httpd.expat == expat;
assert sslSupport -> openssl != null && (httpServer -> httpd.openssl == openssl);
assert pythonBindings -> swig != null && swig.pythonSupport;
assert javaSwigBindings -> swig != null && swig.javaSupport;
assert javahlBindings -> j2sdk != null;

stdenv.mkDerivation {
(name = "subversion-1.1.1";)
...
openssl = if sslSupport then openssl else null;
...
}
19 changes: 19 additions & 0 deletions data/playground/nix/attrsets.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
a = { a = b; c = d; };
a = {
a = b;
c = d;
};

a = {
a = {
b = 1; };

c = { d = [ "1" 2 ] };
};

a = rec {
a = b;
b = a + 1;
};
}
8 changes: 8 additions & 0 deletions data/playground/nix/comments.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Single-line comment
/*
Multi-line comment
*/
{
a = b; # Inline comment (test)
b = c; # Inline comment 2
}
67 changes: 67 additions & 0 deletions data/playground/nix/complex.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
home-manager = {
url = "github:nix-community/home-manager/release-23.05";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { self, nixpkgs, home-manager }:
let
mkHost = hostName: system:
(({ my-config, zfs-root, pkgs, system, ... }:
nixpkgs.lib.nixosSystem {
inherit system;
modules = [
# Module 0: zfs-root and my-config
./modules

# Module 1: host-specific config, if exist
(if (builtins.pathExists
./hosts/${hostName}/configuration.nix) then
(import ./hosts/${hostName}/configuration.nix { inherit pkgs; })
else
{ })

# Module 2: entry point
(({ my-config, zfs-root, pkgs, lib, ... }: {
inherit my-config zfs-root;
system.configurationRevision = if (self ? rev) then
self.rev
else
throw "refuse to build: git tree is dirty";
system.stateVersion = "23.05";
imports = [
"${nixpkgs}/nixos/modules/installer/scan/not-detected.nix"
# "${nixpkgs}/nixos/modules/profiles/hardened.nix"
# "${nixpkgs}/nixos/modules/profiles/qemu-guest.nix"
];
}) {
inherit my-config zfs-root pkgs;
lib = nixpkgs.lib;
})

# Module 3: home-manager
home-manager.nixosModules.home-manager
{
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
}

# Module 4: config shared by all hosts
(import ./configuration.nix { inherit pkgs; })
];
})

# configuration input
(import ./hosts/${hostName} {
system = system;
pkgs = nixpkgs.legacyPackages.${system};
}));
in {
nixosConfigurations = {
exampleHost = mkHost "exampleHost" "x86_64-linux";
};
};
}
13 changes: 13 additions & 0 deletions data/playground/nix/functions.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
# Anonymous function
a = foo: (foo + 1);
x = a: b: a + b;

# Non-built-in function
ba = test( foo: bar: {
b = foo;
});

# Built-in function
x = map (x: y: x + x) [ 1 2 3 ];
}
7 changes: 7 additions & 0 deletions data/playground/nix/lists.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
foo = [ "a" a/b/c "b" ];
bar = [ A
B
C # foo
];
}
36 changes: 36 additions & 0 deletions data/playground/nix/statements.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
key = if a then b else c;
key =
if a
then b
else c;

a = b; # Inline comment (test)
b = c; # Inline comment 2
a = 1 + 1;
a = a/b/c ? 0;
a = b: b + 1;

foo = let
a = b;
c = d;
in
{
output = b;
};

bar = let
a = 1;
b = 2;
in a + b;

bar =
with key;
let
a = key;
in
a;

a = x: x + 1;
b = x: y: x + y + 1;
}
13 changes: 13 additions & 0 deletions data/playground/nix/strings.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
a = "double quoted string";
b = ''two single quote string'';
c = ''
multi-line
quote string'';
d = "Interpolated ${value} string";
e = "Escaped \${string} ";
f = ''
"Nested"
string
'';
}
1 change: 1 addition & 0 deletions packages/common/src/extensionDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const extensionDependencies = [
"scala-lang.scala", // scala
"mrob95.vscode-talonscript", // talon
"jrieken.vscode-tree-sitter-query", // scm
"bbenoist.nix", // nix

// Necessary for the `drink cell` and `pour cell` tests
"ms-toolsai.jupyter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export function findSurroundingPairParseTreeBased(
) {
const document: TextDocument = editor.document;

const individualDelimiters = getIndividualDelimiters(delimiters);
const individualDelimiters = getIndividualDelimiters(
document.languageId,
delimiters,
);

const delimiterTextToDelimiterInfoMap = Object.fromEntries(
individualDelimiters.map((individualDelimiter) => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ export function findSurroundingPairTextBased(
const document: TextDocument = editor.document;
const fullRange = allowableRange ?? document.range;

const individualDelimiters = getIndividualDelimiters(delimiters);
const individualDelimiters = getIndividualDelimiters(
editor.document.languageId,
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
delimiters,
);

const delimiterTextToDelimiterInfoMap = Object.fromEntries(
individualDelimiters.map((individualDelimiter) => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {

type IndividualDelimiterText = string | string[];

export const delimiterToText: Record<
const delimiterToText: Record<
SimpleSurroundingPairName,
[IndividualDelimiterText, IndividualDelimiterText]
> = {
Expand All @@ -25,6 +25,26 @@ export const delimiterToText: Record<
squareBrackets: ["[", "]"],
};

const delimiterToTextNix: Record<
SimpleSurroundingPairName,
[IndividualDelimiterText, IndividualDelimiterText]
> = {
angleBrackets: [
["</", "<"],
[">", "/>"],
],
backtickQuotes: ["`", "`"],
curlyBrackets: [["{", "${"], "}"],
doubleQuotes: ['"', '"'],
escapedDoubleQuotes: ['\\"', '\\"'],
escapedParentheses: ["\\(", "\\)"],
escapedSquareBrackets: ["\\[", "\\]"],
escapedSingleQuotes: ["\\'", "\\'"],
parentheses: [["(", "$("], ")"],
singleQuotes: ["''", "''"],
squareBrackets: ["[", "]"],
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
};

export const leftToRightMap: Record<string, string> = Object.fromEntries(
Object.values(delimiterToText),
);
Expand All @@ -46,3 +66,22 @@ export const complexDelimiterMap: Record<
"angleBrackets",
],
};

/**
* Given a language id, returns a list of all possible delimiters
* for that language.
* @param languageId The language id
* @returns A list of all possible delimiters for that language
*/
export function getSimpleDelimiterMap(
languageId: string,
): Record<
fidgetingbits marked this conversation as resolved.
Show resolved Hide resolved
SimpleSurroundingPairName,
[IndividualDelimiterText, IndividualDelimiterText]
> {
if (languageId == "nix") {
return delimiterToTextNix;
} else {
return delimiterToText;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SimpleSurroundingPairName } from "@cursorless/common";
import { IndividualDelimiter } from "./types";
import { delimiterToText } from "./delimiterMaps";
import { getSimpleDelimiterMap } from "./getDelimiterMaps";
import { concat, uniq } from "lodash";
import { isString } from "../../../util/type";

Expand All @@ -13,8 +13,10 @@ import { isString } from "../../../util/type";
* @returns A list of information about all possible left / right delimiter instances
*/
export function getIndividualDelimiters(
languageId: string,
delimiters: SimpleSurroundingPairName[],
): IndividualDelimiter[] {
const delimiterToText = getSimpleDelimiterMap(languageId);
return delimiters.flatMap((delimiter) => {
const [leftDelimiter, rightDelimiter] = delimiterToText[delimiter];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import getTextFragmentExtractor from "../../../languages/getTextFragmentExtracto
import { Target } from "../../../typings/target.types";
import { SurroundingPairTarget } from "../../targets";
import { getContainingScopeTarget } from "../getContainingScopeTarget";
import { complexDelimiterMap } from "./delimiterMaps";
import { complexDelimiterMap } from "./getDelimiterMaps";
import { SurroundingPairInfo } from "./extractSelectionFromSurroundingPairOffsets";
import { findSurroundingPairParseTreeBased } from "./findSurroundingPairParseTreeBased";
import { findSurroundingPairTextBased } from "./findSurroundingPairTextBased";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: nix
command:
version: 6
spokenForm: change arg
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: argumentOrParameter}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
{ pkgs, ... }:
{ a = 1; }
selections:
- anchor: {line: 0, character: 10}
active: {line: 0, character: 10}
marks: {}
finalState:
documentContents: |-
:
{ a = 1; }
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
languageId: nix
command:
version: 6
spokenForm: change arg
action:
name: clearAndSetSelection
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: argumentOrParameter}
usePrePhraseSnapshot: true
initialState:
documentContents: |-
{ pkgs, ... }:
{ a = 1; }
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
marks: {}
finalState:
documentContents: |-
:
{ a = 1; }
selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
Loading
Loading